Skip to content

Commit b2508f2

Browse files
week12 mission jump-game
1 parent 4f04c9c commit b2508f2

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

โ€Žjump-game/dev-jonghoonpark.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/jump-game/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/07/17/leetcode-55
3+
4+
## dfs๋กœ ํ’€๊ธฐ
5+
6+
```java
7+
class Solution {
8+
boolean canJump = false; // ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ๋„์ฐฉ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด true ๋กœ ๋ณ€๊ฒฝ
9+
10+
public boolean canJump(int[] nums) {
11+
dfs(nums, 0);
12+
13+
return canJump;
14+
}
15+
16+
private void dfs(int[] nums, int pointer) {
17+
// ์œ„์น˜๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์„ ๊ฒฝ์šฐ
18+
// ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์œ„์น˜์ผ ๊ฒฝ์šฐ
19+
// ์ด๋ฏธ ๋งˆ์ง€๋ง‰์— ๋„๋‹ฌ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์„ ๊ฒฝ์šฐ
20+
if (pointer >= nums.length || nums[pointer] == -1 || canJump) {
21+
return;
22+
}
23+
24+
int maxHeight = nums[pointer];
25+
nums[pointer] = -1;
26+
27+
// ๋งˆ์ง€๋ง‰์ด ์•„๋‹Œ๋ฐ 0 ์ด ๋‚˜์™”์„ ๊ฒฝ์šฐ ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ
28+
if (maxHeight == 0 && pointer != nums.length - 1) {
29+
return;
30+
}
31+
32+
if (pointer == nums.length - 1) {
33+
canJump = true;
34+
} else {
35+
while (maxHeight > 0) {
36+
dfs(nums, pointer + maxHeight);
37+
maxHeight--;
38+
}
39+
}
40+
}
41+
}
42+
```
43+
44+
### TC, SC
45+
46+
์‹œ๊ฐ„๋ณต์žก๋„๋Š” `O(n^2)`, ๊ณต๊ฐ„๋ณต์žก๋„๋Š” `O(n)` ์ด๋‹ค.
47+
48+
## dp๋กœ ํ’€๊ธฐ
49+
50+
์ค‘๊ฐ„์— ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜๋Š” ์œ„์น˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. dp ๋ผ๊ณ  ํ•ด๋„ ๋˜๋ ค๋‚˜ ์• ๋งคํ•œ ๊ฒƒ ๊ฐ™๋‹ค.
51+
52+
```java
53+
class Solution {
54+
public boolean canJump(int[] nums) {
55+
int[] dp = new int[nums.length];
56+
int lastIndex = nums.length - 1;
57+
dp[0] = 1;
58+
59+
for (int i = 0; i < nums.length; i++) {
60+
if (dp[i] == 0) {
61+
return false;
62+
}
63+
64+
int current = nums[i];
65+
int toIndex = i + current + 1;
66+
if(toIndex > lastIndex) {
67+
toIndex = nums.length;
68+
}
69+
Arrays.fill(dp, i, toIndex, 1);
70+
if (dp[lastIndex] > 0) {
71+
return true;
72+
}
73+
}
74+
75+
return dp[lastIndex] != 0;
76+
}
77+
}
78+
```
79+
80+
### TC, SC
81+
82+
์‹œ๊ฐ„๋ณต์žก๋„๋Š” `O(n^2)`, ๊ณต๊ฐ„๋ณต์žก๋„๋Š” `O(n)` ์ด๋‹ค.
83+
84+
## greedy ๋ฐฉ์‹์œผ๋กœ ํ’€๊ธฐ
85+
86+
greedy ๋ฌธ์ œ๋Š” ํ•ญ์ƒ ์–ด๋–ป๊ฒŒ ์ฆ๋ช…ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ•ด๋ด์•ผ ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
87+
88+
```java
89+
class Solution {
90+
public boolean canJump(int[] nums) {
91+
int maxReach = 0; // ํ˜„์žฌ๊นŒ์ง€ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋จผ ์ธ๋ฑ์Šค
92+
93+
for (int i = 0; i < nums.length; i++) {
94+
if (i > maxReach) {
95+
return false; // ํ˜„์žฌ ์ธ๋ฑ์Šค์— ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
96+
}
97+
maxReach = Math.max(maxReach, i + nums[i]);
98+
if (maxReach >= nums.length - 1) {
99+
return true; // ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ๋„๋‹ฌํ•˜๊ฑฐ๋‚˜ ๊ทธ ์ด์ƒ์ผ ๊ฒฝ์šฐ
100+
}
101+
}
102+
103+
return false;
104+
}
105+
}
106+
```
107+
108+
### TC, SC
109+
110+
์‹œ๊ฐ„๋ณต์žก๋„๋Š” `O(n)`, ๊ณต๊ฐ„๋ณต์žก๋„๋Š” `O(1)` ์ด๋‹ค.

0 commit comments

Comments
ย (0)