@@ -68,13 +68,13 @@ tags:
68
68
69
69
<!-- solution:start -->
70
70
71
- ### 方法一:路径分析
71
+ ### 方法一:数学
72
72
73
73
我们观察松鼠的移动路径,可以发现,松鼠会首先移动到某个坚果的位置,然后移动到树的位置。接下来,松鼠的移动路径之和等于“其余坚果到树的位置之和”再乘以 $2$。
74
74
75
75
因此,我们只需要选出一个坚果,作为松鼠的第一个目标,使得其到树的位置之和最小,即可得到最小路径。
76
76
77
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为坚果的数量 。
77
+ 时间复杂度 $O(n)$,其中 $n$ 为坚果的数量。 空间复杂度 $O(1)$。
78
78
79
79
<!-- tabs:start -->
80
80
@@ -90,38 +90,39 @@ class Solution:
90
90
squirrel : List[int ],
91
91
nuts : List[List[int ]],
92
92
) -> int :
93
- x, y, a, b = * tree, * squirrel
94
- s = sum (abs (i - x) + abs (j - y) for i, j in nuts) * 2
93
+ tr, tc = tree
94
+ sr, sc = squirrel
95
+ s = sum (abs (r - tr) + abs (c - tc) for r, c in nuts) * 2
95
96
ans = inf
96
- for i, j in nuts:
97
- c = abs (i - x ) + abs (j - y )
98
- d = abs (i - a ) + abs (j - b) + c
99
- ans = min (ans, s + d - c * 2 )
97
+ for r, c in nuts:
98
+ a = abs (r - tr ) + abs (c - tc )
99
+ b = abs (r - sr ) + abs (c - sc)
100
+ ans = min (ans, s - a + b )
100
101
return ans
101
102
```
102
103
103
104
#### Java
104
105
105
106
``` java
107
+ import static java.lang.Math.* ;
108
+
106
109
class Solution {
107
110
public int minDistance (int height , int width , int [] tree , int [] squirrel , int [][] nuts ) {
108
- int ans = Integer . MAX_VALUE ;
111
+ int tr = tree[0 ], tc = tree[1 ];
112
+ int sr = squirrel[0 ], sc = squirrel[1 ];
109
113
int s = 0 ;
110
- for (int [] a : nuts) {
111
- s += f(a, tree );
114
+ for (var e : nuts) {
115
+ s += abs(e[ 0 ] - tr) + abs(e[ 1 ] - tc );
112
116
}
113
- s *= 2 ;
114
- for (int [] a : nuts) {
115
- int c = f(a, tree);
116
- int d = f(a, squirrel) + c;
117
- ans = Math . min(ans, s + d - c * 2 );
117
+ s << = 1 ;
118
+ int ans = Integer . MAX_VALUE ;
119
+ for (var e : nuts) {
120
+ int a = abs(e[0 ] - tr) + abs(e[1 ] - tc);
121
+ int b = abs(e[0 ] - sr) + abs(e[1 ] - sc);
122
+ ans = min(ans, s - a + b);
118
123
}
119
124
return ans;
120
125
}
121
-
122
- private int f (int [] a , int [] b ) {
123
- return Math . abs(a[0 ] - b[0 ]) + Math . abs(a[1 ] - b[1 ]);
124
- }
125
126
}
126
127
```
127
128
@@ -131,43 +132,40 @@ class Solution {
131
132
class Solution {
132
133
public:
133
134
int minDistance(int height, int width, vector<int >& tree, vector<int >& squirrel, vector<vector<int >>& nuts) {
134
- int ans = INT_MAX;
135
+ int tr = tree[ 0] , tc = tree[ 1] ;
136
+ int sr = squirrel[ 0] , sc = squirrel[ 1] ;
135
137
int s = 0;
136
- for (auto& a : nuts) {
137
- s += f(a, tree );
138
+ for (const auto& e : nuts) {
139
+ s += abs(e [ 0 ] - tr) + abs(e [ 1 ] - tc );
138
140
}
139
- s * = 2;
140
- for (auto& a : nuts) {
141
- int c = f(a, tree);
142
- int d = f(a, squirrel) + c;
143
- ans = min(ans, s + d - c * 2);
141
+ s <<= 1;
142
+ int ans = INT_MAX;
143
+ for (const auto& e : nuts) {
144
+ int a = abs(e[ 0] - tr) + abs(e[ 1] - tc);
145
+ int b = abs(e[ 0] - sr) + abs(e[ 1] - sc);
146
+ ans = min(ans, s - a + b);
144
147
}
145
148
return ans;
146
149
}
147
-
148
- int f(vector<int>& a, vector<int>& b) {
149
- return abs(a[0] - b[0]) + abs(a[1] - b[1]);
150
- }
151
150
};
152
151
```
153
152
154
153
#### Go
155
154
156
155
```go
157
156
func minDistance(height int, width int, tree []int, squirrel []int, nuts [][]int) int {
158
- f := func (a, b []int ) int {
159
- return abs (a[0 ]-b[0 ]) + abs (a[1 ]-b[1 ])
160
- }
161
- ans := math.MaxInt32
157
+ tr, tc := tree[0], tree[1]
158
+ sr, sc := squirrel[0], squirrel[1]
162
159
s := 0
163
- for _ , a := range nuts {
164
- s += f (a, tree )
160
+ for _, e := range nuts {
161
+ s += abs(e[0]-tr) + abs(e[1]-tc )
165
162
}
166
- s *= 2
167
- for _ , a := range nuts {
168
- c := f (a, tree)
169
- d := f (a, squirrel) + c
170
- ans = min (ans, s+d-c*2 )
163
+ s <<= 1
164
+ ans := math.MaxInt32
165
+ for _, e := range nuts {
166
+ a := abs(e[0]-tr) + abs(e[1]-tc)
167
+ b := abs(e[0]-sr) + abs(e[1]-sc)
168
+ ans = min(ans, s-a+b)
171
169
}
172
170
return ans
173
171
}
@@ -180,6 +178,86 @@ func abs(x int) int {
180
178
}
181
179
```
182
180
181
+ #### TypeScript
182
+
183
+ ``` ts
184
+ function minDistance(
185
+ height : number ,
186
+ width : number ,
187
+ tree : number [],
188
+ squirrel : number [],
189
+ nuts : number [][],
190
+ ): number {
191
+ const [tr, tc] = tree ;
192
+ const [sr, sc] = squirrel ;
193
+ const s = nuts .reduce ((acc , [r , c ]) => acc + (Math .abs (tr - r ) + Math .abs (tc - c )) * 2 , 0 );
194
+ let ans = Infinity ;
195
+ for (const [r, c] of nuts ) {
196
+ const a = Math .abs (tr - r ) + Math .abs (tc - c );
197
+ const b = Math .abs (sr - r ) + Math .abs (sc - c );
198
+ ans = Math .min (ans , s - a + b );
199
+ }
200
+ return ans ;
201
+ }
202
+ ```
203
+
204
+ #### Rust
205
+
206
+ ``` rust
207
+ impl Solution {
208
+ pub fn min_distance (
209
+ height : i32 ,
210
+ width : i32 ,
211
+ tree : Vec <i32 >,
212
+ squirrel : Vec <i32 >,
213
+ nuts : Vec <Vec <i32 >>,
214
+ ) -> i32 {
215
+ let (tr , tc ) = (tree [0 ], tree [1 ]);
216
+ let (sr , sc ) = (squirrel [0 ], squirrel [1 ]);
217
+ let s : i32 = nuts
218
+ . iter ()
219
+ . map (| nut | (nut [0 ] - tr ). abs () + (nut [1 ] - tc ). abs ())
220
+ . sum :: <i32 >()
221
+ * 2 ;
222
+
223
+ let mut ans = i32 :: MAX ;
224
+ for nut in & nuts {
225
+ let a = (nut [0 ] - tr ). abs () + (nut [1 ] - tc ). abs ();
226
+ let b = (nut [0 ] - sr ). abs () + (nut [1 ] - sc ). abs ();
227
+ ans = ans . min (s - a + b );
228
+ }
229
+
230
+ ans
231
+ }
232
+ }
233
+ ```
234
+
235
+ #### C#
236
+
237
+ ``` cs
238
+ public class Solution {
239
+ public int MinDistance (int height , int width , int [] tree , int [] squirrel , int [][] nuts ) {
240
+ int tr = tree [0 ], tc = tree [1 ];
241
+ int sr = squirrel [0 ], sc = squirrel [1 ];
242
+ int s = 0 ;
243
+
244
+ foreach (var e in nuts ) {
245
+ s += Math .Abs (e [0 ] - tr ) + Math .Abs (e [1 ] - tc );
246
+ }
247
+ s <<= 1 ;
248
+
249
+ int ans = int .MaxValue ;
250
+ foreach (var e in nuts ) {
251
+ int a = Math .Abs (e [0 ] - tr ) + Math .Abs (e [1 ] - tc );
252
+ int b = Math .Abs (e [0 ] - sr ) + Math .Abs (e [1 ] - sc );
253
+ ans = Math .Min (ans , s - a + b );
254
+ }
255
+
256
+ return ans ;
257
+ }
258
+ }
259
+ ```
260
+
183
261
<!-- tabs: end -->
184
262
185
263
<!-- solution: end -->
0 commit comments