|
33 | 33 | */
|
34 | 34 | public class _573 {
|
35 | 35 |
|
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; |
58 | 63 | }
|
59 |
| - return totalDist - savedDist; |
60 |
| - } |
61 | 64 |
|
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 | + } |
64 | 68 | }
|
65 | 69 | }
|
0 commit comments