Skip to content

Commit bf9633f

Browse files
authored
feat: add solutions to lc problem: No.1478 (#4283)
No.1478.Allocate Mailboxes
1 parent d89865f commit bf9633f

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
lines changed

images/leetcode-doocs-old.png

67.6 KB
Loading

images/leetcode-doocs.png

-29.1 KB
Loading

solution/1400-1499/1478.Allocate Mailboxes/README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ $$
9292
其中 $g[i][j]$ 的计算方法如下:
9393

9494
$$
95-
g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i]
95+
g[i][j] = g[i + 1][j - 1] + \textit{houses}[j] - \textit{houses}[i]
9696
$$
9797

9898
时间复杂度 $O(n^2 \times k)$,空间复杂度 $O(n^2)$。其中 $n$ 为房子的数量。
@@ -213,6 +213,39 @@ func minDistance(houses []int, k int) int {
213213
}
214214
```
215215

216+
#### TypeScript
217+
218+
```ts
219+
function minDistance(houses: number[], k: number): number {
220+
houses.sort((a, b) => a - b);
221+
const n = houses.length;
222+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
223+
224+
for (let i = n - 2; i >= 0; i--) {
225+
for (let j = i + 1; j < n; j++) {
226+
g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i];
227+
}
228+
}
229+
230+
const inf = Number.POSITIVE_INFINITY;
231+
const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(inf));
232+
233+
for (let i = 0; i < n; i++) {
234+
f[i][1] = g[0][i];
235+
}
236+
237+
for (let j = 2; j <= k; j++) {
238+
for (let i = j - 1; i < n; i++) {
239+
for (let p = i - 1; p >= 0; p--) {
240+
f[i][j] = Math.min(f[i][j], f[p][j - 1] + g[p + 1][i]);
241+
}
242+
}
243+
}
244+
245+
return f[n - 1][k];
246+
}
247+
```
248+
216249
<!-- tabs:end -->
217250

218251
<!-- solution:end -->

solution/1400-1499/1478.Allocate Mailboxes/README_EN.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tags:
3434
<strong>Input:</strong> houses = [1,4,8,10,20], k = 3
3535
<strong>Output:</strong> 5
3636
<strong>Explanation:</strong> Allocate mailboxes in position 3, 9 and 20.
37-
Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5
37+
Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5
3838
</pre>
3939

4040
<p><strong class="example">Example 2:</strong></p>
@@ -61,7 +61,23 @@ Minimum total distance from each houses to nearest mailboxes is |2-3| + |3-3| +
6161

6262
<!-- solution:start -->
6363

64-
### Solution 1
64+
### Solution 1: Dynamic Programming
65+
66+
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]$.
67+
68+
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:
69+
70+
$$
71+
f[i][j] = \min_{0 \leq p \leq i-1} \{f[p][j-1] + g[p+1][i]\}
72+
$$
73+
74+
where $g[i][j]$ is computed as follows:
75+
76+
$$
77+
g[i][j] = g[i + 1][j - 1] + \textit{houses}[j] - \textit{houses}[i]
78+
$$
79+
80+
The time complexity is $O(n^2 \times k)$, and the space complexity is $O(n^2)$, where $n$ is the number of houses.
6581

6682
<!-- tabs:start -->
6783

@@ -179,6 +195,39 @@ func minDistance(houses []int, k int) int {
179195
}
180196
```
181197

198+
#### TypeScript
199+
200+
```ts
201+
function minDistance(houses: number[], k: number): number {
202+
houses.sort((a, b) => a - b);
203+
const n = houses.length;
204+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
205+
206+
for (let i = n - 2; i >= 0; i--) {
207+
for (let j = i + 1; j < n; j++) {
208+
g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i];
209+
}
210+
}
211+
212+
const inf = Number.POSITIVE_INFINITY;
213+
const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(inf));
214+
215+
for (let i = 0; i < n; i++) {
216+
f[i][1] = g[0][i];
217+
}
218+
219+
for (let j = 2; j <= k; j++) {
220+
for (let i = j - 1; i < n; i++) {
221+
for (let p = i - 1; p >= 0; p--) {
222+
f[i][j] = Math.min(f[i][j], f[p][j - 1] + g[p + 1][i]);
223+
}
224+
}
225+
}
226+
227+
return f[n - 1][k];
228+
}
229+
```
230+
182231
<!-- tabs:end -->
183232

184233
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function minDistance(houses: number[], k: number): number {
2+
houses.sort((a, b) => a - b);
3+
const n = houses.length;
4+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
5+
6+
for (let i = n - 2; i >= 0; i--) {
7+
for (let j = i + 1; j < n; j++) {
8+
g[i][j] = g[i + 1][j - 1] + houses[j] - houses[i];
9+
}
10+
}
11+
12+
const inf = Number.POSITIVE_INFINITY;
13+
const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(inf));
14+
15+
for (let i = 0; i < n; i++) {
16+
f[i][1] = g[0][i];
17+
}
18+
19+
for (let j = 2; j <= k; j++) {
20+
for (let i = j - 1; i < n; i++) {
21+
for (let p = i - 1; p >= 0; p--) {
22+
f[i][j] = Math.min(f[i][j], f[p][j - 1] + g[p + 1][i]);
23+
}
24+
}
25+
}
26+
27+
return f[n - 1][k];
28+
}

0 commit comments

Comments
 (0)