Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6cc532c

Browse files
add 1439
1 parent 895ff9f commit 6cc532c

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+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.TreeSet;
6+
7+
public class _1439 {
8+
public static class Solution1 {
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+
public int kthSmallest(int[][] mat, int k) {
18+
TreeSet<int[]> treeSet = new TreeSet<>(new Comparator<int[]>() {
19+
@Override
20+
public int compare(int[] o1, int[] o2) {
21+
if (o1[0] != o2[0]) {
22+
return o1[0] - o2[0];
23+
} else {
24+
for (int i = 1; i < o1.length; i++) {
25+
if (o1[i] != o2[i]) {
26+
return o1[i] - o2[i];
27+
}
28+
}
29+
return 0;
30+
}
31+
}
32+
});
33+
int m = mat.length;
34+
int n = mat[0].length;
35+
int sum = 0;
36+
int[] entry = new int[m + 1];
37+
for (int i = 0; i < m; i++) {
38+
sum += mat[i][0];
39+
}
40+
entry[0] = sum;
41+
treeSet.add(entry);
42+
int count = 0;
43+
while (count < k) {
44+
int[] curr = treeSet.pollFirst();
45+
count++;
46+
if (count == k) {
47+
return curr[0];
48+
}
49+
for (int i = 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+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1439;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1439Test {
11+
private static _1439.Solution1 solution1;
12+
private static int[][] mat;
13+
private static int expected;
14+
private static int k;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _1439.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
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+
public void test2() {
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+
public void test3() {
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+
public void test4() {
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)