8000 refactor 573 · venkim/Leetcode@5ff0d3d · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 5ff0d3d

Browse files
refactor 573
1 parent 5aa1996 commit 5ff0d3d

File tree

1 file changed

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

1 file changed

+30
-26
lines changed

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,37 @@
3333
*/
3434
public class _573 {
3535

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

62-
private int getDist(int[] pointA, int[] pointB) {
63-
return Math.abs(pointA[0] - pointB[0]) + Math.abs(pointA[1] - pointB[1]);
65+
private int getDist(int[] pointA, int[] pointB) {
66+
return Math.abs(pointA[0] - pointB[0]) + Math.abs(pointA[1] - pointB[1]);
67+
}
6468
}
6569
}

0 commit comments

Comments
 (0)
0