@@ -68,13 +68,13 @@ tags:
6868
6969<!-- solution:start -->
7070
71- ### 方法一:路径分析
71+ ### 方法一:数学
7272
7373我们观察松鼠的移动路径,可以发现,松鼠会首先移动到某个坚果的位置,然后移动到树的位置。接下来,松鼠的移动路径之和等于“其余坚果到树的位置之和”再乘以 $2$。
7474
7575因此,我们只需要选出一个坚果,作为松鼠的第一个目标,使得其到树的位置之和最小,即可得到最小路径。
7676
77- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为坚果的数量 。
77+ 时间复杂度 $O(n)$,其中 $n$ 为坚果的数量。 空间复杂度 $O(1)$。
7878
7979<!-- tabs:start -->
8080
@@ -90,38 +90,39 @@ class Solution:
9090 squirrel : List[int ],
9191 nuts : List[List[int ]],
9292 ) -> 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
9596 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 )
100101 return ans
101102```
102103
103104#### Java
104105
105106``` java
107+ import static java.lang.Math.* ;
108+
106109class Solution {
107110 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 ];
109113 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 );
112116 }
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);
118123 }
119124 return ans;
120125 }
121-
122- private int f (int [] a , int [] b ) {
123- return Math . abs(a[0 ] - b[0 ]) + Math . abs(a[1 ] - b[1 ]);
124- }
125126}
126127```
127128
@@ -131,43 +132,40 @@ class Solution {
131132class Solution {
132133public:
133134 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] ;
135137 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 );
138140 }
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);
144147 }
145148 return ans;
146149 }
147-
148- int f(vector<int>& a, vector<int>& b) {
149- return abs(a[0] - b[0]) + abs(a[1] - b[1]);
150- }
151150};
152151```
153152
154153#### Go
155154
156155```go
157156func 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]
162159 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 )
165162 }
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)
171169 }
172170 return ans
173171}
@@ -180,6 +178,86 @@ func abs(x int) int {
180178}
181179```
182180
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+
183261<!-- tabs: end -->
184262
185263<!-- solution: end -->
0 commit comments