Skip to content

Commit 5ccfa1f

Browse files
committed
Add solution #1306
1 parent 0b20937 commit 5ccfa1f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,219 LeetCode solutions in JavaScript
1+
# 1,220 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -988,6 +988,7 @@
988988
1302|[Deepest Leaves Sum](./solutions/1302-deepest-leaves-sum.js)|Medium|
989989
1304|[Find N Unique Integers Sum up to Zero](./solutions/1304-find-n-unique-integers-sum-up-to-zero.js)|Easy|
990990
1305|[All Elements in Two Binary Search Trees](./solutions/1305-all-elements-in-two-binary-search-trees.js)|Medium|
991+
1306|[Jump Game III](./solutions/1306-jump-game-iii.js)|Medium|
991992
1309|[Decrypt String from Alphabet to Integer Mapping](./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js)|Easy|
992993
1313|[Decompress Run-Length Encoded List](./solutions/1313-decompress-run-length-encoded-list.js)|Easy|
993994
1317|[Convert Integer to the Sum of Two No-Zero Integers](./solutions/1317-convert-integer-to-the-sum-of-two-no-zero-integers.js)|Easy|

solutions/1306-jump-game-iii.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1306. Jump Game III
3+
* https://leetcode.com/problems/jump-game-iii/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of non-negative integers arr, you are initially positioned at start index of
7+
* the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you
8+
* can reach any index with value 0.
9+
*
10+
* Notice that you can not jump outside of the array at any time.
11+
*/
12+
13+
/**
14+
* @param {number[]} arr
15+
* @param {number} start
16+
* @return {boolean}
17+
*/
18+
var canReach = function(arr, start) {
19+
const visited = new Set();
20+
const queue = [start];
21+
22+
while (queue.length) {
23+
const current = queue.shift();
24+
25+
if (arr[current] === 0) return true;
26+
if (visited.has(current)) continue;
27+
28+
visited.add(current);
29+
30+
const jump = arr[current];
31+
const nextRight = current + jump;
32+
const nextLeft = current - jump;
33+
34+
if (nextRight < arr.length) queue.push(nextRight);
35+
if (nextLeft >= 0) queue.push(nextLeft);
36+
}
37+
38+
return false;
39+
};

0 commit comments

Comments
 (0)