diff --git a/images/leetcode-doocs-old.png b/images/leetcode-doocs-old.png new file mode 100644 index 0000000000000..616eea1c01815 Binary files /dev/null and b/images/leetcode-doocs-old.png differ diff --git a/images/leetcode-doocs.png b/images/leetcode-doocs.png index 616eea1c01815..491d500a9eb68 100644 Binary files a/images/leetcode-doocs.png and b/images/leetcode-doocs.png differ diff --git a/solution/1400-1499/1478.Allocate Mailboxes/README.md b/solution/1400-1499/1478.Allocate Mailboxes/README.md index fc0bfc8f8c339..eb3590c396270 100644 --- a/solution/1400-1499/1478.Allocate Mailboxes/README.md +++ b/solution/1400-1499/1478.Allocate Mailboxes/README.md @@ -92,7 +92,7 @@ $$ 其中 $g[i][j]$ 的计算方法如下: $$ -g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i] +g[i][j] = g[i + 1][j - 1] + \textit{houses}[j] - \textit{houses}[i] $$ 时间复杂度 $O(n^2 \times k)$,空间复杂度 $O(n^2)$。其中 $n$ 为房子的数量。 @@ -213,6 +213,39 @@ func minDistance(houses []int, k int) int { } ``` +#### TypeScript + +```ts +function minDistance(houses: number[], k: number): number { + houses.sort((a, b) => a - b); + const n = houses.length; + const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + + for (let i = n - 2; i >= 0; i--) { + for (let j = i + 1; j < n; j++) { + g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i]; + } + } + + const inf = Number.POSITIVE_INFINITY; + const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(inf)); + + for (let i = 0; i < n; i++) { + f[i][1] = g[0][i]; + } + + for (let j = 2; j <= k; j++) { + for (let i = j - 1; i < n; i++) { + for (let p = i - 1; p >= 0; p--) { + f[i][j] = Math.min(f[i][j], f[p][j - 1] + g[p + 1][i]); + } + } + } + + return f[n - 1][k]; +} +``` + diff --git a/solution/1400-1499/1478.Allocate Mailboxes/README_EN.md b/solution/1400-1499/1478.Allocate Mailboxes/README_EN.md index 339356b7ef93b..d61c77c247afc 100644 --- a/solution/1400-1499/1478.Allocate Mailboxes/README_EN.md +++ b/solution/1400-1499/1478.Allocate Mailboxes/README_EN.md @@ -34,7 +34,7 @@ tags: Input: houses = [1,4,8,10,20], k = 3 Output: 5 Explanation: Allocate mailboxes in position 3, 9 and 20. -Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5 +Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5

Example 2:

@@ -61,7 +61,23 @@ Minimum total distance from each houses to nearest mailboxes is |2-3| + |3-3| + -### Solution 1 +### Solution 1: Dynamic Programming + +We define $f[i][j]$ to represent the minimum total distance between the houses and their nearest mailbox, when placing $j$ mailboxes among the first $i+1$ houses. Initially, $f[i][j] = \infty$, and the final answer will be $f[n-1][k]$. + +We can iterate over the last house $p$ controlled by the $j-1$-th mailbox, i.e., $0 \leq p \leq i-1$. The $j$-th mailbox will control the houses in the range $[p+1, \dots, i]$. Let $g[i][j]$ denote the minimum total distance when placing a mailbox for the houses in the range $[i, \dots, j]$. The state transition equation is: + +$$ +f[i][j] = \min_{0 \leq p \leq i-1} \{f[p][j-1] + g[p+1][i]\} +$$ + +where $g[i][j]$ is computed as follows: + +$$ +g[i][j] = g[i + 1][j - 1] + \textit{houses}[j] - \textit{houses}[i] +$$ + +The time complexity is $O(n^2 \times k)$, and the space complexity is $O(n^2)$, where $n$ is the number of houses. @@ -179,6 +195,39 @@ func minDistance(houses []int, k int) int { } ``` +#### TypeScript + +```ts +function minDistance(houses: number[], k: number): number { + houses.sort((a, b) => a - b); + const n = houses.length; + const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + + for (let i = n - 2; i >= 0; i--) { + for (let j = i + 1; j < n; j++) { + g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i]; + } + } + + const inf = Number.POSITIVE_INFINITY; + const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(inf)); + + for (let i = 0; i < n; i++) { + f[i][1] = g[0][i]; + } + + for (let j = 2; j <= k; j++) { + for (let i = j - 1; i < n; i++) { + for (let p = i - 1; p >= 0; p--) { + f[i][j] = Math.min(f[i][j], f[p][j - 1] + g[p + 1][i]); + } + } + } + + return f[n - 1][k]; +} +``` + diff --git a/solution/1400-1499/1478.Allocate Mailboxes/Solution.ts b/solution/1400-1499/1478.Allocate Mailboxes/Solution.ts new file mode 100644 index 0000000000000..df644757f801d --- /dev/null +++ b/solution/1400-1499/1478.Allocate Mailboxes/Solution.ts @@ -0,0 +1,28 @@ +function minDistance(houses: number[], k: number): number { + houses.sort((a, b) => a - b); + const n = houses.length; + const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + + for (let i = n - 2; i >= 0; i--) { + for (let j = i + 1; j < n; j++) { + g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i]; + } + } + + const inf = Number.POSITIVE_INFINITY; + const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(inf)); + + for (let i = 0; i < n; i++) { + f[i][1] = g[0][i]; + } + + for (let j = 2; j <= k; j++) { + for (let i = j - 1; i < n; i++) { + for (let p = i - 1; p >= 0; p--) { + f[i][j] = Math.min(f[i][j], f[p][j - 1] + g[p + 1][i]); + } + } + } + + return f[n - 1][k]; +}