Skip to content

Commit 4fe6fb8

Browse files
add post '(Leetcode) 55 - Jump Game 풀이'
1 parent ba41447 commit 4fe6fb8

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

_posts/2024-07-17-leetcode-55.md

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
layout: post
3+
title: (Leetcode) 55 - Jump Game 풀이
4+
categories: [스터디-알고리즘]
5+
tags:
6+
[
7+
자바,
8+
java,
9+
리트코드,
10+
Leetcode,
11+
알고리즘,
12+
greedy,
13+
dynamic programming,
14+
dp,
15+
dfs,
16+
]
17+
date: 2024-07-17 02:30:00 +0900
18+
toc: true
19+
---
20+
21+
기회가 되어 [달레님의 스터디](https://github.com/DaleStudy/leetcode-study)에 참여하여 시간이 될 때마다 한문제씩 풀어보고 있다.
22+
23+
[https://neetcode.io/practice](https://neetcode.io/practice)
24+
25+
---
26+
27+
[https://leetcode.com/problems/jump-game](https://leetcode.com/problems/jump-game)
28+
29+
## dfs로 풀기
30+
31+
```java
32+
class Solution {
33+
boolean canJump = false; // 마지막 위치에 도착 할 수 있으면 true 로 변경
34+
35+
public boolean canJump(int[] nums) {
36+
dfs(nums, 0);
37+
38+
return canJump;
39+
}
40+
41+
private void dfs(int[] nums, int pointer) {
42+
// 위치가 범위를 벗어났을 경우
43+
// 이미 방문한 위치일 경우
44+
// 이미 마지막에 도달 가능하다는 것을 확인했을 경우
45+
if (pointer >= nums.length || nums[pointer] == -1 || canJump) {
46+
return;
47+
}
48+
49+
int maxHeight = nums[pointer];
50+
nums[pointer] = -1;
51+
52+
// 마지막이 아닌데 0 이 나왔을 경우 이동 불가능
53+
if (maxHeight == 0 && pointer != nums.length - 1) {
54+
return;
55+
}
56+
57+
if (pointer == nums.length - 1) {
58+
canJump = true;
59+
} else {
60+
while (maxHeight > 0) {
61+
dfs(nums, pointer + maxHeight);
62+
maxHeight--;
63+
}
64+
}
65+
}
66+
}
67+
```
68+
69+
### TC, SC
70+
71+
시간복잡도는 `O(n^2)`, 공간복잡도는 `O(n)` 이다.
72+
73+
## dp로 풀기
74+
75+
중간에 도달하지 못하는 위치가 있을 경우 false를 반환한다. dp 라고 해도 되려나 애매한 것 같다.
76+
77+
```java
78+
class Solution {
79+
public boolean canJump(int[] nums) {
80+
int[] dp = new int[nums.length];
81+
int lastIndex = nums.length - 1;
82+
dp[0] = 1;
83+
84+
for (int i = 0; i < nums.length; i++) {
85+
if (dp[i] == 0) {
86+
return false;
87+
}
88+
89+
int current = nums[i];
90+
int toIndex = i + current + 1;
91+
if(toIndex > lastIndex) {
92+
toIndex = nums.length;
93+
}
94+
Arrays.fill(dp, i, toIndex, 1);
95+
if (dp[lastIndex] > 0) {
96+
return true;
97+
}
98+
}
99+
100+
return dp[lastIndex] != 0;
101+
}
102+
}
103+
```
104+
105+
### TC, SC
106+
107+
시간복잡도는 `O(n^2)`, 공간복잡도는 `O(n)` 이다.
108+
109+
## greedy 방식으로 풀기
110+
111+
```java
112+
class Solution {
113+
public boolean canJump(int[] nums) {
114+
int maxReach = 0; // 현재까지 도달할 수 있는 가장 먼 인덱스
115+
116+
for (int i = 0; i < nums.length; i++) {
117+
if (i > maxReach) {
118+
return false; // 현재 인덱스에 도달할 수 없는 경우
119+
}
120+
maxReach = Math.max(maxReach, i + nums[i]);
121+
if (maxReach >= nums.length - 1) {
122+
return true; // 마지막 인덱스에 도달하거나 그 이상일 경우
123+
}
124+
}
125+
126+
return false;
127+
}
128+
}
129+
```
130+
131+
### TC, SC
132+
133+
시간복잡도는 `O(n)`, 공간복잡도는 `O(1)` 이다.
134+
135+
## leetcode에 있던 또 다른 풀이
136+
137+
```java
138+
class Solution {
139+
public boolean canJump(int[] nums) {
140+
int currentIndex = nums.length - 1;
141+
142+
for (int i = nums.length - 2; i >= 0; i--) {
143+
if (i + nums[i] >= currentIndex) {
144+
currentIndex = i;
145+
}
146+
}
147+
148+
return currentIndex == 0;
149+
}
150+
}
151+
```
152+
153+
### TC, SC
154+
155+
시간복잡도는 `O(n)`, 공간복잡도는 `O(1)` 이다.

0 commit comments

Comments
 (0)