Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problem: No.0573 #3950

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

Merged
merged 1 commit into from
Jan 14, 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
164 changes: 121 additions & 43 deletions solution/0500-0599/0573.Squirrel Simulation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ tags:

<!-- solution:start -->

### 方法一:路径分析
### 方法一:数学

我们观察松鼠的移动路径,可以发现,松鼠会首先移动到某个坚果的位置,然后移动到树的位置。接下来,松鼠的移动路径之和等于“其余坚果到树的位置之和”再乘以 $2$。

因此,我们只需要选出一个坚果,作为松鼠的第一个目标,使得其到树的位置之和最小,即可得到最小路径。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为坚果的数量
时间复杂度 $O(n)$,其中 $n$ 为坚果的数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -90,38 +90,39 @@ class Solution:
squirrel: List[int],
nuts: List[List[int]],
) -> int:
x, y, a, b = *tree, *squirrel
s = sum(abs(i - x) + abs(j - y) for i, j in nuts) * 2
tr, tc = tree
sr, sc = squirrel
s = sum(abs(r - tr) + abs(c - tc) for r, c in nuts) * 2
ans = inf
for i, j in nuts:
c = abs(i - x) + abs(j - y)
d = abs(i - a) + abs(j - b) + c
ans = min(ans, s + d - c * 2)
for r, c in nuts:
a = abs(r - tr) + abs(c - tc)
b = abs(r - sr) + abs(c - sc)
ans = min(ans, s - a + b)
return ans
```

#### Java

```java
import static java.lang.Math.*;

class Solution {
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
int ans = Integer.MAX_VALUE;
int tr = tree[0], tc = tree[1];
int sr = squirrel[0], sc = squirrel[1];
int s = 0;
for (int[] a : nuts) {
s += f(a, tree);
for (var e : nuts) {
s += abs(e[0] - tr) + abs(e[1] - tc);
}
s *= 2;
for (int[] a : nuts) {
int c = f(a, tree);
int d = f(a, squirrel) + c;
ans = Math.min(ans, s + d - c * 2);
s <<= 1;
int ans = Integer.MAX_VALUE;
for (var e : nuts) {
int a = abs(e[0] - tr) + abs(e[1] - tc);
int b = abs(e[0] - sr) + abs(e[1] - sc);
ans = min(ans, s - a + b);
}
return ans;
}

private int f(int[] a, int[] b) {
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
}
}
```

Expand All @@ -131,43 +132,40 @@ class Solution {
class Solution {
public:
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
int ans = INT_MAX;
int tr = tree[0], tc = tree[1];
int sr = squirrel[0], sc = squirrel[1];
int s = 0;
for (auto& a : nuts) {
s += f(a, tree);
for (const auto& e : nuts) {
s += abs(e[0] - tr) + abs(e[1] - tc);
}
s *= 2;
for (auto& a : nuts) {
int c = f(a, tree);
int d = f(a, squirrel) + c;
ans = min(ans, s + d - c * 2);
s <<= 1;
int ans = INT_MAX;
for (const auto& e : nuts) {
int a = abs(e[0] - tr) + abs(e[1] - tc);
int b = abs(e[0] - sr) + abs(e[1] - sc);
ans = min(ans, s - a + b);
}
return ans;
}

int f(vector<int>& a, vector<int>& b) {
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
}
};
```

#### Go

```go
func minDistance(height int, width int, tree []int, squirrel []int, nuts [][]int) int {
f := func(a, b []int) int {
return abs(a[0]-b[0]) + abs(a[1]-b[1])
}
ans := math.MaxInt32
tr, tc := tree[0], tree[1]
sr, sc := squirrel[0], squirrel[1]
s := 0
for _, a := range nuts {
s += f(a, tree)
for _, e := range nuts {
s += abs(e[0]-tr) + abs(e[1]-tc)
}
s *= 2
for _, a := range nuts {
c := f(a, tree)
d := f(a, squirrel) + c
ans = min(ans, s+d-c*2)
s <<= 1
ans := math.MaxInt32
for _, e := range nuts {
a := abs(e[0]-tr) + abs(e[1]-tc)
b := abs(e[0]-sr) + abs(e[1]-sc)
ans = min(ans, s-a+b)
}
return ans
}
Expand All @@ -180,6 +178,86 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function minDistance(
height: number,
width: number,
tree: number[],
squirrel: number[],
nuts: number[][],
): number {
const [tr, tc] = tree;
const [sr, sc] = squirrel;
const s = nuts.reduce((acc, [r, c]) => acc + (Math.abs(tr - r) + Math.abs(tc - c)) * 2, 0);
let ans = Infinity;
for (const [r, c] of nuts) {
const a = Math.abs(tr - r) + Math.abs(tc - c);
const b = Math.abs(sr - r) + Math.abs(sc - c);
ans = Math.min(ans, s - a + b);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn min_distance(
height: i32,
width: i32,
tree: Vec<i32>,
squirrel: Vec<i32>,
nuts: Vec<Vec<i32>>,
) -> i32 {
let (tr, tc) = (tree[0], tree[1]);
let (sr, sc) = (squirrel[0], squirrel[1]);
let s: i32 = nuts
.iter()
.map(|nut| (nut[0] - tr).abs() + (nut[1] - tc).abs())
.sum::<i32>()
* 2;

let mut ans = i32::MAX;
for nut in &nuts {
let a = (nut[0] - tr).abs() + (nut[1] - tc).abs();
let b = (nut[0] - sr).abs() + (nut[1] - sc).abs();
ans = ans.min(s - a + b);
}

ans
}
}
```

#### C#

```cs
public class Solution {
public int MinDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
int tr = tree[0], tc = tree[1];
int sr = squirrel[0], sc = squirrel[1];
int s = 0;

foreach (var e in nuts) {
s += Math.Abs(e[0] - tr) + Math.Abs(e[1] - tc);
}
s <<= 1;

int ans = int.MaxValue;
foreach (var e in nuts) {
int a = Math.Abs(e[0] - tr) + Math.Abs(e[1] - tc);
int b = Math.Abs(e[0] - sr) + Math.Abs(e[1] - sc);
ans = Math.Min(ans, s - a + b);
}

return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading