Skip to content

Commit d5e98d4

Browse files
committed
jump-game solution
1 parent 798ef63 commit d5e98d4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

โ€Žjump-game/jdy8739.jsโ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var canJump = function (nums) {
6+
const dp = new Array(nums.length).fill(false);
7+
8+
dp[0] = true;
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
if (!dp[i]) {
12+
continue;
13+
}
14+
15+
for (let j = 1; j < nums[i] + 1; j++) {
16+
if (i + j >= nums.length) {
17+
break;
18+
}
19+
20+
dp[i + j] = true;
21+
}
22+
}
23+
24+
return dp[dp.length - 1];
25+
};
26+
27+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n2) -> nums์˜ ๊ธธ์ด๋Œ€๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ 1๋ถ€ํ„ฐ nums[j]์˜ ๊ฐ’๊นŒ์ง€ ์ˆœํšŒํ•˜๋Š” 2์ค‘ ๋ฃจํ”„๋ฅผ ๋Œ๊ธฐ ๋•Œ๋ฌธ
28+
// ๊ณต๊ฐ„๋ณต์žก๋„ 0(n) -> nums์˜ ๊ธธ์ด์— ๋”ฐ๋ผ dp ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฒฐ์ •๋˜๋ฏ€๋กœ

0 commit comments

Comments
ย (0)