|
3 | 3 | /**
|
4 | 4 | * 573. Squirrel Simulation
|
5 | 5 | *
|
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. |
7 | 13 |
|
8 | 14 | Example 1:
|
9 | 15 |
|
|
27 | 33 | */
|
28 | 34 | public class _573 {
|
29 | 35 |
|
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 | + * */ |
31 | 52 | public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
|
32 | 53 | int totalDist = 0;
|
33 | 54 | int savedDist = Integer.MIN_VALUE;
|
34 | 55 | 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 |
36 | 57 | savedDist = Math.max(savedDist, getDist(nut, tree) - getDist(nut, squirrel));
|
37 | 58 | }
|
38 | 59 | return totalDist - savedDist;
|
39 | 60 | }
|
40 | 61 |
|
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]); |
43 | 64 | }
|
44 | 65 | }
|
0 commit comments