Skip to content

Commit 0d14788

Browse files
committed
jump-game solution
1 parent d67575a commit 0d14788

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

β€Žjump-game/byol-han.jsβ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* https://leetcode.com/problems/jump-game/description/
3+
* @param {number[]} nums
4+
* @return {boolean}
5+
*/
6+
var canJump = function (nums) {
7+
let maxReach = 0; // ν˜„μž¬κΉŒμ§€ 도달할 수 μžˆλŠ” μ΅œλŒ€ 인덱슀
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
// ν˜„μž¬ μœ„μΉ˜κ°€ 도달 κ°€λŠ₯ν•œ μ΅œλŒ€ 거리보닀 λ©€λ‹€λ©΄ 끝에 도달할 수 μ—†μŒ
11+
if (i > maxReach) {
12+
return false;
13+
}
14+
15+
// ν˜„μž¬ μœ„μΉ˜μ—μ„œ 갈 수 μžˆλŠ” μ΅œλŒ€ μœ„μΉ˜ μ—…λ°μ΄νŠΈ
16+
maxReach = Math.max(maxReach, i + nums[i]);
17+
18+
// μ΅œμ’… λͺ©μ μ§€μ— 도달 κ°€λŠ₯ν•˜λ©΄ true λ°”λ‘œ λ°˜ν™˜
19+
if (maxReach >= nums.length - 1) {
20+
return true;
21+
}
22+
}
23+
24+
// 반볡문 λλ‚˜λ„ λͺ» λ„λ‹¬ν•œ 경우
25+
return false;
26+
};

0 commit comments

Comments
Β (0)