8000 add a solution for 378 · Algorithm-box/Leetcode@58f4bfa · GitHub
[go: up one dir, main page]

Skip to content
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

Appearance settings

Commit 58f4bfa

Browse files
add a solution for 378
1 parent 2f77c80 commit 58f4bfa

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/main/java/com/fishercoder/solutions/_378.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.List;
6+
import java.util.PriorityQueue;
67

78
public class _378 {
89
public static class Solution1 {
@@ -52,4 +53,24 @@ public int kthSmallest(int[][] matrix, int k) {
5253
return lo;
5354
}
5455
}
56+
57+
public static class Solution3 {
58+
/**
59+
* use heap data structure
60+
*/
61+
public int kthSmallest(int[][] matrix, int k) {
62+
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
63+
for (int i = 0; i < matrix.length; i++) {
64+
//we store value, rowIndex, colIndex as an array into this heap
65+
heap.offer(new int[]{matrix[i][0], i, 0});
66+
}
67+
while (k-- > 1) {
68+
int[] min = heap.poll();
69+
if (min[2] + 1 < matrix[min[1]].length) {
70+
heap.offer(new int[]{matrix[min[1]][min[2] + 1], min[1], min[2] + 1});
71+
}
72+
}
73+
return heap.poll()[0];
74+
}
75+
}
5576
}

src/test/java/com/fishercoder/_378Test.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
public class _378Test {
1111
private static _378.Solution1 solution1;
1212
private static _378.Solution2 solution2;
13+
private static _378.Solution3 solution3;
1314
private static int[][] matrix;
1415

1516
@BeforeClass
1617
public static void setup() {
1718
solution1 = new _378.Solution1();
1819
solution2 = new _378.Solution2();
20+
solution3 = new _378.Solution3();
1921
}
2022

2123
@Test
@@ -25,13 +27,23 @@ public void test1() {
2527
};
2628
assertEquals(-5, solution1.kthSmallest(matrix, 1));
2729
assertEquals(-5, solution2.kthSmallest(matrix, 1));
30+
assertEquals(-5, solution3.kthSmallest(matrix, 1));
2831
}
2932

3033
@Test
3134
public void test2() {
3235
matrix = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[1,3]");
3336
assertEquals(1, solution1.kthSmallest(matrix, 2));
3437
assertEquals(1, solution2.kthSmallest(matrix, 2));
38+
assertEquals(1, solution3.kthSmallest(matrix, 2));
39+
}
40+
41+
@Test
42+
public void test3() {
43+
matrix = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,5,9],[10,11,13],[12,13,15]");
44+
assertEquals(13, solution1.kthSmallest(matrix, 8));
45+
assertEquals(13, solution2.kthSmallest(matrix, 8));
46+
assertEquals(13, solution3.kthSmallest(matrix, 8));
3547
}
3648

3749
}

0 commit comments

Comments
 (0)
0