diff --git a/package-lock.json b/package-lock.json index f76149179e..fe714e4eb0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "docsify-cli": "^4.4.4" }, "devDependencies": { - "prettier": "2.8.8" + "prettier": "^2.8.8" } }, "node_modules/@sindresorhus/is": { @@ -1333,7 +1333,7 @@ }, "node_modules/prettier": { "version": "2.8.8", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", + "resolved": "https://registry.npmmirror.com/prettier/-/prettier-2.8.8.tgz", "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", "dev": true, "bin": { @@ -1341,9 +1341,6 @@ }, "engines": { "node": ">=10.13.0" - }, - "funding": { - "url": "https://github.com/prettier/prettier?sponsor=1" } }, "node_modules/prismjs": { diff --git a/package.json b/package.json index ecf9e80f1c..7976703c80 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,6 @@ "docsify-cli": "^4.4.4" }, "devDependencies": { - "prettier": "2.8.8" + "prettier": "^2.8.8" } } diff --git a/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README.md b/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README.md index 42de4704d7..643df0f232 100644 --- a/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README.md +++ b/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README.md @@ -56,11 +56,17 @@ -**方法一:暴力枚举** +**方法一:逐行遍历 + 排序** -注意到 $k$ 的值不超过 $200$,我们逐层遍历,每一层最多保留 $k$ 个数,然后与下一层的 $n$ 个数累加,排序。 +根据题目描述,我们需要找出前 $m$ 行的所有可能数组中的第 $k$ 个最小数组和。 -最后返回第 $k$ 个数即可。 +如果我们能够找出前 $m - 1$ 行的所有可能数组中的前 $k$ 个最小数组和,那么我们可以将第 $m$ 行的每个元素与前 $m - 1$ 行的前 $k$ 个最小数组和相加,将得到的所有结果排序后,取前 $k$ 个最小值,即为前 $m$ 行的所有可能数组中的前 $k$ 个最小值。 + +因此,我们可以定义一个数组 $pre$,用于存储此前遍历到的行的前 $k$ 个最小数组和,初始时 $pre$ 只有一个元素 $0$。 + +然后,我们遍历 $mat$ 的每一行 $cur$,将 $cur$ 中的每个元素与 $pre$ 中的每个元素相加,将得到的所有结果排序后,取前 $k$ 个最小值作为新的 $pre$。继续遍历下一行,直到遍历完所有行。 + +最后返回 $pre$ 中的第 $k$ 个数(下标 $k-1$)即可。 时间复杂度 $O(m \times n \times k \times \log (n \times k))$,空间复杂度 $O(n \times k)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 @@ -164,6 +170,24 @@ func min(a, b int) int { } ``` +### **TypeScript** + +```ts +function kthSmallest(mat: number[][], k: number): number { + let pre: number[] = [0]; + for (const cur of mat) { + const next: number[] = []; + for (const a of pre) { + for (const b of cur) { + next.push(a + b); + } + } + pre = next.sort((a, b) => a - b).slice(0, k); + } + return pre[k - 1]; +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README_EN.md b/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README_EN.md index 356bf017bb..c877e720b5 100644 --- a/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README_EN.md +++ b/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README_EN.md @@ -146,6 +146,24 @@ func min(a, b int) int { } ``` +### **TypeScript** + +```ts +function kthSmallest(mat: number[][], k: number): number { + let pre: number[] = [0]; + for (const cur of mat) { + const next: number[] = []; + for (const a of pre) { + for (const b of cur) { + next.push(a + b); + } + } + pre = next.sort((a, b) => a - b).slice(0, k); + } + return pre[k - 1]; +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/Solution.ts b/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/Solution.ts new file mode 100644 index 0000000000..37d92311df --- /dev/null +++ b/solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/Solution.ts @@ -0,0 +1,13 @@ +function kthSmallest(mat: number[][], k: number): number { + let pre: number[] = [0]; + for (const cur of mat) { + const next: number[] = []; + for (const a of pre) { + for (const b of cur) { + next.push(a + b); + } + } + pre = next.sort((a, b) => a - b).slice(0, k); + } + return pre[k - 1]; +}