diff --git a/solution/0500-0599/0573.Squirrel Simulation/README.md b/solution/0500-0599/0573.Squirrel Simulation/README.md index a3530da45971c..4015581064b18 100644 --- a/solution/0500-0599/0573.Squirrel Simulation/README.md +++ b/solution/0500-0599/0573.Squirrel Simulation/README.md @@ -68,13 +68,13 @@ tags: -### 方法一:路径分析 +### 方法一:数学 我们观察松鼠的移动路径,可以发现,松鼠会首先移动到某个坚果的位置,然后移动到树的位置。接下来,松鼠的移动路径之和等于“其余坚果到树的位置之和”再乘以 $2$。 因此,我们只需要选出一个坚果,作为松鼠的第一个目标,使得其到树的位置之和最小,即可得到最小路径。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为坚果的数量。 +时间复杂度 $O(n)$,其中 $n$ 为坚果的数量。空间复杂度 $O(1)$。 @@ -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]); - } } ``` @@ -131,23 +132,21 @@ class Solution { class Solution { public: int minDistance(int height, int width, vector& tree, vector& squirrel, vector>& 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& a, vector& b) { - return abs(a[0] - b[0]) + abs(a[1] - b[1]); - } }; ``` @@ -155,19 +154,18 @@ public: ```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 } @@ -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, + squirrel: Vec, + nuts: Vec>, + ) -> 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::() + * 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; + } +} +``` + diff --git a/solution/0500-0599/0573.Squirrel Simulation/README_EN.md b/solution/0500-0599/0573.Squirrel Simulation/README_EN.md index dfadb00d58c65..4131743bcefd8 100644 --- a/solution/0500-0599/0573.Squirrel Simulation/README_EN.md +++ b/solution/0500-0599/0573.Squirrel Simulation/README_EN.md @@ -66,7 +66,13 @@ tags: -### Solution 1 +### Solution 1: Mathematics + +Observing the squirrel's movement path, we can see that the squirrel will first move to the position of a nut, then move to the position of the tree. After that, the total movement path of the squirrel is equal to "the sum of the distances from the remaining nuts to the tree" multiplied by $2$. + +Therefore, we only need to select a nut as the squirrel's first target, such that the sum of its distance to the tree is minimized, to obtain the shortest path. + +The time complexity is $O(n)$, where $n$ is the number of nuts. The space complexity is $O(1)$. @@ -82,38 +88,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]); - } } ``` @@ -123,23 +130,21 @@ class Solution { class Solution { public: int minDistance(int height, int width, vector& tree, vector& squirrel, vector>& 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& a, vector& b) { - return abs(a[0] - b[0]) + abs(a[1] - b[1]); - } }; ``` @@ -147,19 +152,18 @@ public: ```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 } @@ -172,6 +176,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, + squirrel: Vec, + nuts: Vec>, + ) -> 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::() + * 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; + } +} +``` + diff --git a/solution/0500-0599/0573.Squirrel Simulation/Solution.cpp b/solution/0500-0599/0573.Squirrel Simulation/Solution.cpp index f83493d426832..3286dcdb8adc6 100644 --- a/solution/0500-0599/0573.Squirrel Simulation/Solution.cpp +++ b/solution/0500-0599/0573.Squirrel Simulation/Solution.cpp @@ -1,21 +1,19 @@ class Solution { public: int minDistance(int height, int width, vector& tree, vector& squirrel, vector>& 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& a, vector& b) { - return abs(a[0] - b[0]) + abs(a[1] - b[1]); - } -}; \ No newline at end of file +}; diff --git a/solution/0500-0599/0573.Squirrel Simulation/Solution.cs b/solution/0500-0599/0573.Squirrel Simulation/Solution.cs new file mode 100644 index 0000000000000..926e2bcb1b12f --- /dev/null +++ b/solution/0500-0599/0573.Squirrel Simulation/Solution.cs @@ -0,0 +1,21 @@ +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; + } +} diff --git a/solution/0500-0599/0573.Squirrel Simulation/Solution.go b/solution/0500-0599/0573.Squirrel Simulation/Solution.go index 841eccfd25fa8..1606d9efe7fb8 100644 --- a/solution/0500-0599/0573.Squirrel Simulation/Solution.go +++ b/solution/0500-0599/0573.Squirrel Simulation/Solution.go @@ -1,17 +1,16 @@ 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 } @@ -21,4 +20,4 @@ func abs(x int) int { return -x } return x -} \ No newline at end of file +} diff --git a/solution/0500-0599/0573.Squirrel Simulation/Solution.java b/solution/0500-0599/0573.Squirrel Simulation/Solution.java index 8031deca629ec..123c475b561ee 100644 --- a/solution/0500-0599/0573.Squirrel Simulation/Solution.java +++ b/solution/0500-0599/0573.Squirrel Simulation/Solution.java @@ -1,20 +1,20 @@ +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]); - } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0573.Squirrel Simulation/Solution.py b/solution/0500-0599/0573.Squirrel Simulation/Solution.py index 92c401f3b2fc1..9bcc6086c6ec7 100644 --- a/solution/0500-0599/0573.Squirrel Simulation/Solution.py +++ b/solution/0500-0599/0573.Squirrel Simulation/Solution.py @@ -7,11 +7,12 @@ def minDistance( 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 diff --git a/solution/0500-0599/0573.Squirrel Simulation/Solution.rs b/solution/0500-0599/0573.Squirrel Simulation/Solution.rs new file mode 100644 index 0000000000000..c6601dcf66eb5 --- /dev/null +++ b/solution/0500-0599/0573.Squirrel Simulation/Solution.rs @@ -0,0 +1,26 @@ +impl Solution { + pub fn min_distance( + height: i32, + width: i32, + tree: Vec, + squirrel: Vec, + nuts: Vec>, + ) -> 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::() + * 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 + } +} diff --git a/solution/0500-0599/0573.Squirrel Simulation/Solution.ts b/solution/0500-0599/0573.Squirrel Simulation/Solution.ts new file mode 100644 index 0000000000000..0f13523ed4924 --- /dev/null +++ b/solution/0500-0599/0573.Squirrel Simulation/Solution.ts @@ -0,0 +1,18 @@ +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; +}