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

Commit58f4bfa

Browse files
add a solution for 378
1 parent2f77c80 commit58f4bfa

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
importjava.util.ArrayList;
44
importjava.util.Collections;
55
importjava.util.List;
6+
importjava.util.PriorityQueue;
67

78
publicclass_378 {
89
publicstaticclassSolution1 {
@@ -52,4 +53,24 @@ public int kthSmallest(int[][] matrix, int k) {
5253
returnlo;
5354
}
5455
}
56+
57+
publicstaticclassSolution3 {
58+
/**
59+
* use heap data structure
60+
*/
61+
publicintkthSmallest(int[][]matrix,intk) {
62+
PriorityQueue<int[]>heap =newPriorityQueue<>((a,b) ->a[0] -b[0]);
63+
for (inti =0;i <matrix.length;i++) {
64+
//we store value, rowIndex, colIndex as an array into this heap
65+
heap.offer(newint[]{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(newint[]{matrix[min[1]][min[2] +1],min[1],min[2] +1});
71+
}
72+
}
73+
returnheap.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
publicclass_378Test {
1111
privatestatic_378.Solution1solution1;
1212
privatestatic_378.Solution2solution2;
13+
privatestatic_378.Solution3solution3;
1314
privatestaticint[][]matrix;
1415

1516
@BeforeClass
1617
publicstaticvoidsetup() {
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
publicvoidtest2() {
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+
publicvoidtest3() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp