Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1478 #4283

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added images/leetcode-doocs-old.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/leetcode-doocs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 34 additions & 1 deletion solution/1400-1499/1478.Allocate Mailboxes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 为房子的数量。
Expand Down Expand Up @@ -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];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
53 changes: 51 additions & 2 deletions solution/1400-1499/1478.Allocate Mailboxes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tags:
<strong>Input:</strong> houses = [1,4,8,10,20], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> 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
</pre>

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

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
28 changes: 28 additions & 0 deletions solution/1400-1499/1478.Allocate Mailboxes/Solution.ts
Original file line number Diff line number Diff line change
@@ -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];
}