Skip to content

Commit 336c346

Browse files
committed
Add solution #1439
1 parent a91c22a commit 336c346

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,303 LeetCode solutions in JavaScript
1+
# 1,304 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1098,6 +1098,7 @@
10981098
1436|[Destination City](./solutions/1436-destination-city.js)|Easy|
10991099
1437|[Check If All 1's Are at Least Length K Places Away](./solutions/1437-check-if-all-1s-are-at-least-length-k-places-away.js)|Easy|
11001100
1438|[Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](./solutions/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js)|Medium|
1101+
1439|[Find the Kth Smallest Sum of a Matrix With Sorted Rows](./solutions/1439-find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js)|Hard|
11011102
1443|[Minimum Time to Collect All Apples in a Tree](./solutions/1443-minimum-time-to-collect-all-apples-in-a-tree.js)|Medium|
11021103
1446|[Consecutive Characters](./solutions/1446-consecutive-characters.js)|Easy|
11031104
1447|[Simplified Fractions](./solutions/1447-simplified-fractions.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows
3+
* https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/
4+
* Difficulty: Hard
5+
*
6+
* You are given an m x n matrix mat that has its rows sorted in non-decreasing order and
7+
* an integer k.
8+
*
9+
* You are allowed to choose exactly one element from each row to form an array.
10+
*
11+
* Return the kth smallest array sum among all possible arrays.
12+
*/
13+
14+
/**
15+
* @param {number[][]} mat
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var kthSmallest = function(mat, k) {
20+
let sums = [0];
21+
const rows = mat.length;
22+
23+
for (let row = 0; row < rows; row++) {
24+
const nextSums = [];
25+
for (const prevSum of sums) {
26+
for (const num of mat[row]) {
27+
nextSums.push(prevSum + num);
28+
}
29+
}
30+
sums = nextSums.sort((a, b) => a - b).slice(0, Math.min(k, nextSums.length));
31+
}
32+
33+
return sums[k - 1];
34+
};

0 commit comments

Comments
 (0)