-
-
Notifications
You must be signed in to change notification settings - Fork 248
[박종훈] 12주차 답안 제출 #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[박종훈] 12주차 답안 제출 #187
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e92686e
week12 mission unique-paths
dev-jonghoonpark 4f04c9c
week12 mission longest-common-subsequence
dev-jonghoonpark b2508f2
week12 mission jump-game
dev-jonghoonpark 666e72c
week12 mission maximum-subarray
dev-jonghoonpark e0cdd7a
week12 mission longest-increasing-subsequence
dev-jonghoonpark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
- 문제: https://leetcode.com/problems/jump-game/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/07/17/leetcode-55 | ||
|
||
## dfs로 풀기 | ||
|
||
```java | ||
class Solution { | ||
boolean canJump = false; // 마지막 위치에 도착 할 수 있으면 true 로 변경 | ||
|
||
public boolean canJump(int[] nums) { | ||
dfs(nums, 0); | ||
|
||
return canJump; | ||
} | ||
|
||
private void dfs(int[] nums, int pointer) { | ||
// 위치가 범위를 벗어났을 경우 | ||
// 이미 방문한 위치일 경우 | ||
// 이미 마지막에 도달 가능하다는 것을 확인했을 경우 | ||
if (pointer >= nums.length || nums[pointer] == -1 || canJump) { | ||
return; | ||
} | ||
|
||
int maxHeight = nums[pointer]; | ||
nums[pointer] = -1; | ||
|
||
// 마지막이 아닌데 0 이 나왔을 경우 이동 불가능 | ||
if (maxHeight == 0 && pointer != nums.length - 1) { | ||
return; | ||
} | ||
|
||
if (pointer == nums.length - 1) { | ||
canJump = true; | ||
} else { | ||
while (maxHeight > 0) { | ||
dfs(nums, pointer + maxHeight); | ||
maxHeight--; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간복잡도는 `O(n^2)`, 공간복잡도는 `O(n)` 이다. | ||
|
||
## dp로 풀기 | ||
|
||
중간에 도달하지 못하는 위치가 있을 경우 false를 반환한다. dp 라고 해도 되려나 애매한 것 같다. | ||
|
||
```java | ||
class Solution { | ||
public boolean canJump(int[] nums) { | ||
int[] dp = new int[nums.length]; | ||
int lastIndex = nums.length - 1; | ||
dp[0] = 1; | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (dp[i] == 0) { | ||
return false; | ||
} | ||
|
||
int current = nums[i]; | ||
int toIndex = i + current + 1; | ||
if(toIndex > lastIndex) { | ||
toIndex = nums.length; | ||
} | ||
Arrays.fill(dp, i, toIndex, 1); | ||
if (dp[lastIndex] > 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return dp[lastIndex] != 0; | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간복잡도는 `O(n^2)`, 공간복잡도는 `O(n)` 이다. | ||
|
||
## greedy 방식으로 풀기 | ||
|
||
greedy 문제는 항상 어떻게 증명할 수 있는지를 고민을 많이 해봐야 하는 것 같다. | ||
|
||
```java | ||
class Solution { | ||
public boolean canJump(int[] nums) { | ||
int maxReach = 0; // 현재까지 도달할 수 있는 가장 먼 인덱스 | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (i > maxReach) { | ||
return false; // 현재 인덱스에 도달할 수 없는 경우 | ||
} | ||
maxReach = Math.max(maxReach, i + nums[i]); | ||
if (maxReach >= nums.length - 1) { | ||
return true; // 마지막 인덱스에 도달하거나 그 이상일 경우 | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간복잡도는 `O(n)`, 공간복잡도는 `O(1)` 이다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
- 문제: https://leetcode.com/problems/longest-common-subsequence/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/07/17/leetcode-1143 | ||
|
||
```java | ||
class Solution { | ||
public int longestCommonSubsequence(String text1, String text2) { | ||
int[][] dp = new int[text1.length() + 1][text2.length() + 1]; | ||
|
||
for (int i = 1; i < dp.length; i++) { | ||
for (int j = 1; j < dp[0].length; j++) { | ||
if (text1.charAt(i - 1) == text2.charAt(j - 1)) { | ||
dp[i][j] = dp[i - 1][j - 1] + 1; | ||
} else { | ||
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); | ||
} | ||
} | ||
} | ||
|
||
return dp[text1.length()][text2.length()]; | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 `O(m * n)` 공간 복잡도는 `O(m * n)` 이다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
- 문제: https://leetcode.com/problems/longest-increasing-subsequence/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/02/27/leetcode-300 | ||
|
||
Beats 62.23% | ||
|
||
```java | ||
class Solution { | ||
public int lengthOfLIS(int[] nums) { | ||
int[] dp = new int[nums.length]; | ||
Arrays.fill(dp, 1); | ||
|
||
int max = 1; | ||
for (int i = 1; i < nums.length; i++) { | ||
for (int j = 0; j < i; j++) { | ||
if (nums[j] < nums[i]) { | ||
dp[i] = Math.max(dp[j] + 1, dp[i]); | ||
} | ||
} | ||
max = Math.max(max, dp[i]); | ||
} | ||
return max; | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간복잡도는 O(n^2), 공간복잡도는 O(n)이다. | ||
|
||
## Follow up 문제 | ||
|
||
> Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity? | ||
|
||
파이썬 에서는 bisect_left 를 쓰면 된다고 하지만 자바에는 존재하지 않는다. 하지만 방법을 찾아보자. | ||
|
||
### dp 를 사용하지 않은 풀이 (Beats 82.20%) | ||
|
||
우선은 ArrayList를 이용하여 비슷하게 모방해보았다. | ||
|
||
```java | ||
public int lengthOfLIS(int[] nums) { | ||
ArrayList<Integer> subsequence = new ArrayList<>(); | ||
subsequence.add(nums[0]); | ||
|
||
for (int i = 1; i < nums.length; i++) { | ||
int current = nums[i]; | ||
|
||
if(current > subsequence.get(subsequence.size() - 1)) { | ||
subsequence.addLast(current); | ||
} else if (current < subsequence.get(0)) { | ||
subsequence.set(0, current); | ||
} else { | ||
for (int j = 1; j < subsequence.size(); j++) { | ||
if(current > subsequence.get(j - 1) && current < subsequence.get(j)) { | ||
subsequence.set(j, current); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return subsequence.size(); | ||
} | ||
``` | ||
|
||
#### TC, SC | ||
|
||
아직은 여전히 시간복잡도는 O(n^2), 공간복잡도는 O(n)이다. | ||
빅오 표기법 상으로는 동일하나 실제 동작 시간은 감소하였다. | ||
|
||
### 진짜 binary search를 도입해보기 (Beats 92.57%) | ||
|
||
자바 Collections 에서는 binarySearch 메소드를 제공해준다. | ||
적용해보면 다음과 같다. | ||
|
||
```java | ||
public int lengthOfLIS(int[] nums) { | ||
ArrayList<Integer> subsequence = new ArrayList<>(); | ||
|
||
for (int current : nums) { | ||
// Collections.binarySearch : 목록에 포함된 경우 검색 키의 인덱스, 그렇지 않으면 (-(삽입점) - 1) 을 반환함. | ||
int pos = Collections.binarySearch(subsequence, current); | ||
if (pos < 0) pos = -(pos + 1); | ||
if (pos >= subsequence.size()) { | ||
subsequence.add(current); | ||
} else { | ||
subsequence.set(pos, current); | ||
} | ||
} | ||
|
||
return subsequence.size(); | ||
} | ||
``` | ||
|
||
#### Collections.binarySearch | ||
|
||
해당 메소드의 리턴값은 다음과 같다. | ||
|
||
> 목록에 포함된 경우 검색 키의 인덱스, 그렇지 않으면 (-(삽입점) - 1). | ||
> 삽입 지점은 키가 목록에 삽입되는 지점, 즉 키보다 큰 첫 번째 요소의 인덱스 또는 목록의 모든 요소가 지정된 키보다 작은 경우 list.size()로 정의됩니다. | ||
> 키가 발견되는 경우에만 반환값이 >= 0이 되도록 보장합니다. | ||
|
||
#### TC, SC | ||
|
||
시간복잡도는 `O(n * logn)`, 공간복잡도는 `O(n)`이다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
- 문제: https://leetcode.com/problems/maximum-subarray/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/05/07/leetcode-53 | ||
|
||
```java | ||
class Solution { | ||
public int maxSubArray(int[] nums) { | ||
int maxSum = Integer.MIN_VALUE; | ||
int currentSum = 0; | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
currentSum += nums[i]; | ||
maxSum = Math.max(maxSum, currentSum); | ||
currentSum = Math.max(currentSum, 0); | ||
} | ||
|
||
return maxSum; | ||
} | ||
} | ||
``` | ||
|
||
- currentSum이 maxSum보다 클 경우 maxSum을 갱신한다. | ||
- currentSum은 음수일 경우 (0보다 작을 경우) 0으로 초기화 한다. | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 O(n), 공간 복잡도는 O(1) 이다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
- 문제: https://leetcode.com/problems/unique-paths/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/07/16/leetcode-62 | ||
|
||
```java | ||
class Solution { | ||
public int uniquePaths(int m, int n) { | ||
int[][] matrix = new int[m][n]; | ||
matrix[0][0] = 1; | ||
|
||
Deque<Coordinate> deque = new ArrayDeque<>(); | ||
deque.addLast(new Coordinate(1, 0)); | ||
deque.addLast(new Coordinate(0, 1)); | ||
while (!deque.isEmpty()) { | ||
Coordinate coordinate = deque.removeFirst(); | ||
if (coordinate.x >= m || coordinate.y >= n || matrix[coordinate.x][coordinate.y] != 0) { | ||
continue; | ||
} | ||
|
||
int top = coordinate.y > 0 ? matrix[coordinate.x][coordinate.y - 1] : 0; | ||
int left = coordinate.x > 0 ? matrix[coordinate.x - 1][coordinate.y] : 0; | ||
matrix[coordinate.x][coordinate.y] = top + left; | ||
|
||
deque.addLast(new Coordinate(coordinate.x + 1, coordinate.y)); | ||
deque.addLast(new Coordinate(coordinate.x, coordinate.y + 1)); | ||
} | ||
|
||
return matrix[m - 1][n - 1]; | ||
} | ||
} | ||
|
||
class Coordinate { | ||
int x; | ||
int y; | ||
|
||
public Coordinate(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 `O(m * n)` 공간 복잡도는 `O(m * n)` 이다. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와, 종훈님의 이 풀이를 보고 와 이렇게 접근할 수도 있구나 충격을 받았습니다. 덕분에 이 문제를 다시 풀고 알고달레에 풀이 5와 풀이 6을 추가하게 되었네요. 신선한 영감을 주셔서 감사합니다! 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DaleSeo 칭찬 감사드립니다 : ) 🥰