|
| 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 16: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 | +greedy 문제는 항상 어떻게 증명할 수 있는지를 고민을 많이 해봐야 하는 것 같다. |
| 112 | + |
| 113 | +```java |
| 114 | +class Solution { |
| 115 | + public boolean canJump(int[] nums) { |
| 116 | + int maxReach = 0; // 현재까지 도달할 수 있는 가장 먼 인덱스 |
| 117 | + |
| 118 | + for (int i = 0; i < nums.length; i++) { |
| 119 | + if (i > maxReach) { |
| 120 | + return false; // 현재 인덱스에 도달할 수 없는 경우 |
| 121 | + } |
| 122 | + maxReach = Math.max(maxReach, i + nums[i]); |
| 123 | + if (maxReach >= nums.length - 1) { |
| 124 | + return true; // 마지막 인덱스에 도달하거나 그 이상일 경우 |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return false; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### TC, SC |
| 134 | + |
| 135 | +시간복잡도는 `O(n)`, 공간복잡도는 `O(1)` 이다. |
| 136 | + |
| 137 | +## leetcode에 있던 또 다른 풀이 |
| 138 | + |
| 139 | +뒤에서 부터 푸는게 신기해서 가져왔다. |
| 140 | + |
| 141 | +```java |
| 142 | +class Solution { |
| 143 | + public boolean canJump(int[] nums) { |
| 144 | + int currentIndex = nums.length - 1; |
| 145 | + |
| 146 | + for (int i = nums.length - 2; i >= 0; i--) { |
| 147 | + if (i + nums[i] >= currentIndex) { |
| 148 | + currentIndex = i; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return currentIndex == 0; |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### TC, SC |
| 158 | + |
| 159 | +시간복잡도는 `O(n)`, 공간복잡도는 `O(1)` 이다. |
0 commit comments