Skip to content

Commit 9d404d9

Browse files
authored
Create Find the Kth Smallest Sum of a Matrix With Sorted Rows.java
1 parent 03835d3 commit 9d404d9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int kthSmallest(int[][] mat, int k) {
3+
PriorityQueue<Integer> prevSum = new PriorityQueue<>(Collections.reverseOrder());
4+
prevSum.add(0);
5+
for (int[] row : mat) {
6+
PriorityQueue<Integer> nextSum = new PriorityQueue<>(Collections.reverseOrder());
7+
for (int sum : prevSum) {
8+
for (int c = 0; c < mat[0].length; c++) {
9+
nextSum.add(sum + row[c]);
10+
if (nextSum.size() > k) {
11+
nextSum.poll();
12+
}
13+
}
14+
}
15+
prevSum = nextSum;
16+
}
17+
return prevSum.poll();
18+
}
19+
}

0 commit comments

Comments
 (0)