Skip to content

Commit fe7cf9b

Browse files
refactor 573
1 parent f5d77a3 commit fe7cf9b

File tree

1 file changed

+26
-5
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+26
-5
lines changed

src/main/java/com/fishercoder/solutions/_573.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
/**
44
* 573. Squirrel Simulation
55
*
6-
* There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Your goal is to find the minimal distance for the squirrel to collect all the nuts and put them under the tree one by one. The squirrel can only take at most one nut at one time and can move in four directions - up, down, left and right, to the adjacent cell. The distance is represented by the number of moves.
6+
* There's a tree, a squirrel, and several nuts.
7+
* Positions are represented by the cells in a 2D grid.
8+
* Your goal is to find the minimal distance for the squirrel to collect all the nuts and
9+
* put them under the tree one by one.
10+
* The squirrel can only take at most one nut at one time and can move in four directions -
11+
* up, down, left and right, to the adjacent cell.
12+
* The distance is represented by the number of moves.
713
814
Example 1:
915
@@ -27,18 +33,33 @@
2733
*/
2834
public class _573 {
2935

30-
/**credit: https://leetcode.com/articles/squirrel-simulation*/
36+
/**reference: https://leetcode.com/articles/squirrel-simulation
37+
*
38+
* 1. The order in which to pick the nuts does not matter except the first one
39+
* because for all the other nuts, the squirrel needs to travel back and forth.
40+
*
41+
* 2. For the first nut to be picked, there's some distance we might be able to save, what is this distance?
42+
* It is the difference between the squirrel's original starting point to the first nut and that the distance from this
43+
* first nut to the tree.
44+
* This is because, only for the first nut, the squirrel does NOT need to travel back and forth, it only needs to travel from
45+
* its starting position to the nut position and then return to the tree.
46+
*
47+
* 3. For the rest of all other nuts, the squirrel always needs to go back and forth.
48+
*
49+
* 4. So how can we find the first nut to go to so that we could have the maximum saved distance?
50+
* We iterate through all of the nuts and keep updating the savedDist as below:
51+
* */
3152
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
3253
int totalDist = 0;
3354
int savedDist = Integer.MIN_VALUE;
3455
for (int[] nut : nuts) {
35-
totalDist += (getDist(nut, tree) * 2);
56+
totalDist += (getDist(nut, tree) * 2);//it needs to travel back and forth, so times two
3657
savedDist = Math.max(savedDist, getDist(nut, tree) - getDist(nut, squirrel));
3758
}
3859
return totalDist - savedDist;
3960
}
4061

41-
private int getDist(int[] nut, int[] tree) {
42-
return Math.abs(nut[0] - tree[0]) + Math.abs(nut[1] - tree[1]);
62+
private int getDist(int[] pointA, int[] pointB) {
63+
return Math.abs(pointA[0] - pointB[0]) + Math.abs(pointA[1] - pointB[1]);
4364
}
4465
}

0 commit comments

Comments
 (0)