8000 [LEET-505] add 505 · FitzroyCM/Leetcode@8f39df3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f39df3

Browse files
[LEET-505] add 505
1 parent 63c8cb6 commit 8f39df3

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindLargestValueinEachTreeRow.java) | O(n) |O(k) | Medium| BFS
77
|513|[Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindBottomLeftValue.java) | O(n) |O(k) | Medium| BFS
88
|506|[Relative Ranks](https://leetcode.com/problems/relative-ranks/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RelativeRanks.java) | O(nlogn) |O(n) | Easy|
9+
|505|[The Maze II](https://leetcode.com/problems/the-maze-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/TheMazeII.java) | O(m*n) |O(m*n) | Medium| BFS
910
|504|[Base 7](https://leetcode.com/problems/base-7/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Base7.java) | O(1) |O(1) | Easy|
1011
|501|[Find Mode in Binary Tree](https://leetcode.com/problems/find-mode-in-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindModeinBinaryTree.java) | O(n) |O(k) | Easy| Binary Tree
1112
|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/KeyboardRow.java) | O(n) |O(1) | Easy|
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
8+
9+
Given the ball's start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces t 10000 raveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.
10+
11+
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
12+
13+
Example 1
14+
15+
Input 1: a maze represented by a 2D array
16+
17+
0 0 1 0 0
18+
0 0 0 0 0
19+
0 0 0 1 0
20+
1 1 0 1 1
21+
0 0 0 0 0
22+
23+
Input 2: start coordinate (rowStart, colStart) = (0, 4)
24+
Input 3: destination coordinate (rowDest, colDest) = (4, 4)
25+
26+
Output: 12
27+
Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right.
28+
The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
29+
30+
Example 2
31+
32+
Input 1: a maze represented by a 2D array
33+
34+
0 0 1 0 0
35+
0 0 0 0 0
36+
0 0 0 1 0
37+
1 1 0 1 1
38+
0 0 0 0 0
39+
40+
Input 2: start coordinate (rowStart, colStart) = (0, 4)
41+
Input 3: destination coordinate (rowDest, colDest) = (3, 2)
42+
43+
Output: -1
44+
Explanation: There is no way for the ball to stop at the destination.
45+
46+
Note:
47+
There is only one ball and one destination in the maze.
48+
Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
49+
The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
50+
The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
51+
*/
52+
public class TheMazeII {
53+
54+
/**The difference between II and I of this problem:
55+
* the extra array is not boolean type any more, but int type, and it's recording the length of each point to start point.*/
56+
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
57+
58+
final int[] directions = new int[]{-1, 0, 1, 0, -1};
59+
Queue<Point> queue = new LinkedList<>();
60+
queue.offer(new Point(start[0], start[1], 0));
61+
int m = maze.length, n = maze[0].length;
62+
int[][] length = new int[m][n];
63+
for (int i = 0; i < m*n; i++) length[i/n][i%n] = Integer.MAX_VALUE;//initialize the length array
64+
65+
while (!queue.isEmpty()) {
66+
Point curr = queue.poll();
67+
int x = curr.x, y = curr.y, distance = curr.distance;//keep the original value
68+
if (length[x][y] <= distance) continue;
69+
length[x][y] = distance;
70+
for (int i = 0; i < directions.length - 1; i++) {
71+
int xx = curr.x, yy = curr.y, distanceDistance = curr.distance;//use temp variables to move
72+
//we need below while loop to find only "stop" points that could be put into the queue
73+
while (xx >= 0 && yy >= 0 && xx < m && yy < n && maze[xx][yy] == 0) {
74+
xx += directions[i];
75+
yy += directions[i + 1];
76+
distanceDistance++;
77+
}
78+
xx -= directions[i];
79+
yy -= directions[i + 1];
80+
distanceDistance--;
81+
queue.offer(new Point(xx, yy, distanceDistance));
82+
}
83+
}
84+
return length[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : length[destination[0]][destination[1]];
85+
86+
}
87+
88+
class Point {
89+
int x;
90+
int y;
91+
int distance;
92+
93+
public Point(int x, int y, int distance) {
94+
this.x = x;
95+
this.y = y;
96+
this.distance = distance;
97+
}
98+
}
99+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.TheMazeII;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
public class TheMazeIITest {
11+
private static TheMazeII test;
12+
private static int expected;
13+
private static int actual;
14+
private static int[][] maze;
15+
private static int[] start;
16+
private static int[] destination;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new TheMazeII();
21+
}
22+
23+
@Before
24+
public void setupForEachTest(){}
25+
26+
@Test
27+
public void test1(){
28+
maze = new int[][]{
29+
{0,0,0,0,0},
30+
{1,1,0,0,1},
31+
{0,0,0,0,0},
32+
{0,1,0,0,1},
33+
{0,1,0,0,0}
34+
};
35+
start = new int[]{4,3};
36+
destination = new int[]{0,1};
37+
actual = test.shortestDistance(maze, start, destination);
38+
A712 expected = -1;
39+
assertEquals(expected, actual);
40+
41+
}
42+
43+
@Test
44+
public void test2(){
45+
maze = new int[][]{
46+
{0,0,1,0,0},
47+
{0,0,0,0,0},
48+
{0,0,0,1,0},
49+
{1,1,0,1,1},
50+
{0,0,0,0,0}
51+
};
52+
start = new int[]{0,4};
53+
destination = new int[]{4,4};
54+
actual = test.shortestDistance(maze, start, destination);
55+
expected = 12;
56+
assertEquals(expected, actual);
57+
}
58+
}

0 commit comments

Comments
 (0)
0