Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6cc532c

Browse files
add 1439
1 parent895ff9f commit6cc532c

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ _If you like this project, please leave me a star._ ★
269269
|1447|[Simplified Fractions](https://leetcode.com/problems/simplified-fractions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1447.java)||Medium|Math|
270270
|1446|[Consecutive Characters](https://leetcode.com/problems/consecutive-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1446.java)||Easy|String|
271271
|1441|[Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1441.java)||Easy|Stack|
272+
|1439|[Find the Kth Smallest Sum of a Matrix With Sorted Rows](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1439.java)||Hard|Array, Binary Search, PriorityQueue, Matrix|
272273
|1437|[Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1437.java)||Medium|Array|
273274
|1436|[Destination City](https://leetcode.com/problems/destination-city/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1436.java)||Easy|String|
274275
|1432|[Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1432.java)||Medium|String|
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
importjava.util.Comparator;
5+
importjava.util.TreeSet;
6+
7+
publicclass_1439 {
8+
publicstaticclassSolution1 {
9+
/**
10+
* My completely own implementation after reading the hint on LeetCode:
11+
* 1. We put the sum along with every single element's index in each row as an array into a TreeSet, let it sort by its sum ascendingly;
12+
* each time we poll an element out of the set, we can find the next m smallest sums by moving each row index to the right by one.
13+
* 2. priority queue doesn't help in this case unless used in combination with a set to filter out duplicates;
14+
* 3. implement a customized comparator for treeset: if sum i.e. entry[0] doesn't equal, then it's not a duplicate,
15+
* then we compare the rest of the elements in the array, as long as anyone of them differs at the same index, it's not a duplicate.
16+
*/
17+
publicintkthSmallest(int[][]mat,intk) {
18+
TreeSet<int[]>treeSet =newTreeSet<>(newComparator<int[]>() {
19+
@Override
20+
publicintcompare(int[]o1,int[]o2) {
21+
if (o1[0] !=o2[0]) {
22+
returno1[0] -o2[0];
23+
}else {
24+
for (inti =1;i <o1.length;i++) {
25+
if (o1[i] !=o2[i]) {
26+
returno1[i] -o2[i];
27+
}
28+
}
29+
return0;
30+
}
31+
}
32+
});
33+
intm =mat.length;
34+
intn =mat[0].length;
35+
intsum =0;
36+
int[]entry =newint[m +1];
37+
for (inti =0;i <m;i++) {
38+
sum +=mat[i][0];
39+
}
40+
entry[0] =sum;
41+
treeSet.add(entry);
42+
intcount =0;
43+
while (count <k) {
44+
int[]curr =treeSet.pollFirst();
45+
count++;
46+
if (count ==k) {
47+
returncurr[0];
48+
}
49+
for (inti =0;i <m;i++) {
50+
int[]next =Arrays.copyOf(curr,curr.length);
51+
if (curr[i +1] +1 <n) {
52+
next[0] -=mat[i][curr[i +1]];
53+
next[0] +=mat[i][curr[i +1] +1];
54+
next[i +1]++;
55+
treeSet.add(next);
56+
}
57+
}
58+
}
59+
return -1;
60+
}
61+
}
62+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1439;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_1439Test {
11+
privatestatic_1439.Solution1solution1;
12+
privatestaticint[][]mat;
13+
privatestaticintexpected;
14+
privatestaticintk;
15+
16+
@BeforeClass
17+
publicstaticvoidsetup() {
18+
solution1 =new_1439.Solution1();
19+
}
20+
21+
@Test
22+
publicvoidtest1() {
23+
mat =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3,11],[2,4,6]");
24+
expected =7;
25+
k =5;
26+
assertEquals(expected,solution1.kthSmallest(mat,k));
27+
}
28+
29+
@Test
30+
publicvoidtest2() {
31+
mat =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3,11],[2,4,6]");
32+
expected =17;
33+
k =9;
34+
assertEquals(expected,solution1.kthSmallest(mat,k));
35+
}
36+
37+
@Test
38+
publicvoidtest3() {
39+
mat =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,10,10],[1,4,5],[2,3,6]");
40+
expected =9;
41+
k =7;
42+
assertEquals(expected,solution1.kthSmallest(mat,k));
43+
}
44+
45+
@Test
46+
publicvoidtest4() {
47+
mat =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,1,10],[2,2,9]");
48+
expected =12;
49+
k =7;
50+
assertEquals(expected,solution1.kthSmallest(mat,k));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp