-
-
Notifications
You must be signed in to change notification settings - Fork 245
[eunhwa99] Week 04 solutions #1367
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46756eb
merged two sorted lists
eunhwa99 0899a76
Merge remote-tracking branch 'origin/main'
eunhwa99 9dc43f4
maximum depth of binary tree
eunhwa99 3635b2a
find minimum in rotated sorted array
eunhwa99 c86248a
word search
eunhwa99 8f94cc3
coin change
eunhwa99 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,24 @@ | ||
import java.util.Arrays; | ||
|
||
// 시간 복잡도: O(n * m) - n: 동전의 개수, m: 금액 | ||
// 공간 복잡도: O(n) - dp 배열 사용 | ||
class Solution { | ||
|
||
public int coinChange(int[] coins, int amount) { | ||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, amount + 1); | ||
dp[0] = 0; | ||
|
||
for (int i = 1; i <= amount; i++) { | ||
for (int coin : coins) { | ||
if (i - coin >= 0) { | ||
dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] == amount + 1 ? -1 : dp[amount]; | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -1,17 +1,21 @@ | ||
class Solution { | ||
// TC: O(log N) | ||
// SC: O(1) | ||
// 시간 복잡도: O(log N) - Binary search | ||
// 공간 복잡도: O(1) - Constant extra space | ||
public int findMin(int[] nums) { | ||
int left = 0; | ||
int right = nums.length - 1; | ||
while (left < right) { | ||
int mid = left + (right - left) / 2; | ||
if (nums[mid] < nums[right]) { | ||
right = mid; | ||
int low = 0; | ||
int high = nums.length - 1; | ||
|
||
while (low < high) { | ||
int mid = low + (high - low) / 2; | ||
|
||
if (nums[mid] < nums[high]) { | ||
high = mid; | ||
} else { | ||
left = mid + 1; | ||
low = mid + 1; | ||
} | ||
} | ||
return nums[left]; | ||
|
||
return nums[low]; | ||
} | ||
} | ||
|
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,14 @@ | ||
public class eunhwa99 { | ||
|
||
// 시간 복잡도: O(n) - 트리를 한 번 순회 | ||
// 공간 복잡도: O(h) - 재귀 호출 스택 공간 (h는 트리의 높이) | ||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) return 0; // 빈 노드인 경우 깊이는 0 | ||
int leftDepth = maxDepth(root.left); // 왼쪽 서브트리 깊이 | ||
int rightDepth = maxDepth(root.right); // 오른쪽 서브트리 깊이 | ||
return Math.max(leftDepth, rightDepth) + 1; // 최대 깊이 + 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,34 @@ | ||
public class eunhwa99 { | ||
|
||
// 시간 복잡도: O(n) - 두 개의 리스트를 한 번씩 순회 | ||
// 공간 복잡도: O(1) - 추가적인 공간 사용 없음 | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
if (list1 == null) return list2; | ||
if (list2 == null) return list1; | ||
|
||
ListNode dummy = new ListNode(0); | ||
ListNode current = dummy; | ||
|
||
while (list1 != null && list2 != null) { | ||
if (list1.val <= list2.val) { | ||
current.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
current.next = list2; | ||
list2 = list2.next; | ||
} | ||
current = current.next; | ||
} | ||
|
||
if (list1 != null) { | ||
current.next = list1; | ||
} else { | ||
current.next = list2; | ||
} | ||
|
||
return dummy.next; | ||
} | ||
} | ||
} | ||
|
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,43 @@ | ||
class Solution { | ||
|
||
// 시간 복잡도: O(m * n * 4^L) - m: 행, n: 열, L: 단어 길이 | ||
// 공간 복잡도: O(m * n) - 방문 체크를 위한 공간 | ||
public boolean exist(char[][] board, String word) { | ||
int rows = board.length; | ||
int cols = board[0].length; | ||
|
||
for (int r = 0; r < rows; r++) { | ||
for (int c = 0; c < cols; c++) { | ||
if (dfs(board, word, 0, r, c)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private boolean dfs(char[][] board, String word, int index, int r, int c) { | ||
if (index == word.length()) { | ||
return true; | ||
} | ||
|
||
if (r < 0 || r >= board.length || c < 0 || c >= board[0].length || board[r][c] != word.charAt( | ||
index)) { | ||
return false; | ||
} | ||
|
||
char temp = board[r][c]; | ||
board[r][c] = '#'; | ||
|
||
boolean found = | ||
dfs(board, word, index + 1, r + 1, c) || | ||
dfs(board, word, index + 1, r - 1, c) || | ||
dfs(board, word, index + 1, r, c + 1) || | ||
dfs(board, word, index + 1, r, c - 1); | ||
|
||
board[r][c] = temp; | ||
return found; | ||
} | ||
} | ||
|
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.