Skip to content

Commit f0398d7

Browse files
authored
Create 403-frog-jump.js
1 parent 7798136 commit f0398d7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

403-frog-jump.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} stones
3+
* @return {boolean}
4+
*/
5+
const canCross = function(stones) {
6+
for (let i = 3; i < stones.length; i++) {
7+
if (stones[i] > stones[i - 1] * 2) {
8+
return false
9+
}
10+
}
11+
let count = new Set(stones)
12+
let lastStone = stones[stones.length - 1]
13+
let position = [0]
14+
let jump = [0]
15+
while (position.length > 0) {
16+
let nextPosition = position.pop()
17+
let nextDistance = jump.pop()
18+
for (let i = nextDistance - 1; i <= nextDistance + 1; i++) {
19+
if (i <= 0) {
20+
continue
21+
}
22+
let nextStone = nextPosition + i
23+
if (nextStone == lastStone) {
24+
return true
25+
} else if (count.has(nextStone)) {
26+
position.push(nextStone)
27+
jump.push(i)
28+
}
29+
}
30+
}
31+
return false
32+
}

0 commit comments

Comments
 (0)