|
| 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