Skip to content

Commit 6ff5d00

Browse files
committed
Add solution #505
1 parent df82d68 commit 6ff5d00

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@
493493
502|[IPO](./solutions/0502-ipo.js)|Hard|
494494
503|[Next Greater Element II](./solutions/0503-next-greater-element-ii.js)|Medium|
495495
504|[Base 7](./solutions/0504-base-7.js)|Easy|
496+
505|[The Maze II](./solutions/0505-the-maze-ii.js)|Medium|
496497
506|[Relative Ranks](./solutions/0506-relative-ranks.js)|Easy|
497498
507|[Perfect Number](./solutions/0507-perfect-number.js)|Easy|
498499
508|[Most Frequent Subtree Sum](./solutions/0508-most-frequent-subtree-sum.js)|Medium|

solutions/0505-the-maze-ii.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* 505. The Maze II
3+
* https://leetcode.com/problems/the-maze-ii/
4+
* Difficulty: Medium
5+
*
6+
* There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1).
7+
* The ball can go through the empty spaces by rolling up, down, left or right, but it won't
8+
* stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
9+
*
10+
* Given the m x n maze, the ball's start position and the destination, where
11+
* start = [startrow, startcol] and destination = [destinationrow, destinationcol], return
12+
* the shortest distance for the ball to stop at the destination. If the ball cannot stop
13+
* at destination, return -1.
14+
*
15+
* The distance is the number of empty spaces traveled by the ball from the start position
16+
* (excluded) to the destination (included).
17+
*
18+
* You may assume that the borders of the maze are all walls (see examples).
19+
*/
20+
21+
/**
22+
* @param {number[][]} maze
23+
* @param {number[]} start
24+
* @param {number[]} destination
25+
* @return {number}
26+
*/
27+
var shortestDistance = function(maze, start, destination) {
28+
const rows = maze.length;
29+
const cols = maze[0].length;
30+
const distances = new Array(rows).fill().map(() => new Array(cols).fill(Infinity));
31+
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
32+
const queue = [[start[0], start[1], 0]];
33+
distances[start[0]][start[1]] = 0;
34+
35+
while (queue.length) {
36+
const [row, col, distance] = queue.shift();
37+
if (distance > distances[row][col]) continue;
38+
39+
for (const [dx, dy] of directions) {
40+
let nextRow = row;
41+
let nextCol = col;
42+
let steps = 0;
43+
44+
while (nextRow + dx >= 0 && nextRow + dx < rows && nextCol + dy >= 0
45+
&& nextCol + dy < cols && maze[nextRow + dx][nextCol + dy] === 0
46+
) {
47+
nextRow += dx;
48+
nextCol += dy;
49+
steps++;
50+
}
51+
52+
const newDistance = distance + steps;
53+
if (newDistance < distances[nextRow][nextCol]) {
54+
distances[nextRow][nextCol] = newDistance;
55+
queue.push([nextRow, nextCol, newDistance]);
56+
}
57+
}
58+
}
59+
60+
const result = distances[destination[0]][destination[1]];
61+
return result === Infinity ? -1 : result;
62+
};

0 commit comments

Comments
 (0)