Skip to content

Commit 00be0fc

Browse files
committed
add solution : 55. Jump Game
1 parent e940453 commit 00be0fc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

β€Žjump-game/mmyeon.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*@link https://leetcode.com/problems/jump-game/description/
3+
*
4+
* μ ‘κ·Ό 방법 :
5+
* - ν˜„μž¬ μΈλ±μŠ€μ—μ„œ μ΅œλŒ€λ‘œ 도달할 수 μžˆλŠ” 인덱슀λ₯Ό κ°±μ‹ ν•˜μ—¬ λ§ˆμ§€λ§‰ μΈλ±μŠ€μ— 도달할 수 μžˆλŠ”μ§€ 체크
6+
* - μ΅œλŒ€ 도달할 수 μžˆλŠ” μΈλ±μŠ€κ°€ ν˜„μž¬ μΈλ±μŠ€λ³΄λ‹€ μž‘μœΌλ©΄, μ΄ν›„λŠ” 확인할 ν•„μš” μ—†μœΌλ‹ˆκΉŒ false 리턴
7+
*
8+
* μ‹œκ°„λ³΅μž‘λ„ : O(n)
9+
* - n = λ°°μ—΄μ˜ 길이, λ°°μ—΄ 1회만 순회
10+
*
11+
* κ³΅κ°„λ³΅μž‘λ„ : O(1)
12+
* - κ³ μ •λœ λ³€μˆ˜λ§Œ μ‚¬μš©
13+
*/
14+
15+
function canJump(nums: number[]): boolean {
16+
const lastIndex = nums.length - 1;
17+
let maxReachableIndex = 0;
18+
19+
for (let i = 0; i < nums.length; i++) {
20+
if (maxReachableIndex < i) return false;
21+
22+
maxReachableIndex = Math.max(maxReachableIndex, i + nums[i]);
23+
24+
if (lastIndex <= maxReachableIndex) return true;
25+
}
26+
27+
return false;
28+
}

0 commit comments

Comments
Β (0)