-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jinhyungrhee] Week 04 Solutions #1362
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
5 commits
Select commit
Hold shift + click to select a range
5fe9b18
add solution of maximum-depth-of-binary-tree
jinhyungrhee ed599cd
add solution of merge-two-sorted-lists
jinhyungrhee 8dbd72d
add solution of find-minimum-in-rotated-sorted-array
jinhyungrhee 6cd9569
add solution of coin-change
jinhyungrhee a03c011
add solution of word-search
jinhyungrhee 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,22 @@ | ||
import java.util.*; | ||
class Solution { | ||
int INF = 987654321; | ||
public int coinChange(int[] coins, int amount) { | ||
if (amount == 0) return 0; | ||
|
||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, INF); | ||
|
||
dp[0] = 0; | ||
for (int coin : coins) { | ||
if (coin <= amount) dp[coin] = 1; | ||
} | ||
|
||
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] == INF) ? -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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public int findMin(int[] nums) { | ||
int left = 0, right = nums.length - 1; | ||
// 오름차순인 경우 | ||
if (nums[left] < nums[right]) return nums[left]; | ||
// 오름차순이 아닌 경우 이진탐색 | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
if (nums[mid] < nums[right]) { | ||
right = mid; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
return nums[left]; | ||
} | ||
} |
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,17 @@ | ||
class Solution { | ||
public int maxVal; | ||
public int maxDepth(TreeNode root) { | ||
dfs(root, 0); | ||
return maxVal; | ||
} | ||
public void dfs(TreeNode node, int step) { | ||
|
||
if (node == null) { | ||
if (maxVal < step) maxVal = step; | ||
return; | ||
} | ||
|
||
dfs(node.left, step + 1); | ||
dfs(node.right, step + 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,29 @@ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
|
||
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; | ||
} | ||
if (list2 != null) { | ||
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,40 @@ | ||
class Solution { | ||
public boolean exist(char[][] board, String word) { | ||
|
||
boolean[][] visited = new boolean[board.length][board[0].length]; | ||
|
||
for (int i = 0; i < board.length; i++) { | ||
for (int j = 0; j < board[0].length; j++) { | ||
if(dfs(board, word, visited, i, j, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public boolean dfs(char[][] board, String word, boolean[][] visited, int x, int y, int index) { | ||
|
||
// [성공] : 모든 글자 매칭 성공 | ||
if (index == word.length()) return true; | ||
|
||
// [실패] : 범위 초과, 방문함, 글자 불일치 | ||
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || | ||
visited[x][y] || board[x][y] != word.charAt(index)) { | ||
return false; | ||
} | ||
|
||
visited[x][y] = true; | ||
int[][] dir = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; | ||
for (int i = 0; i < 4; i++) { | ||
int newX = x + dir[i][0], newY = y + dir[i][1]; | ||
if (dfs(board, word, visited, newX, newY, index + 1)) { | ||
return true; // 찾았으면 바로 리턴 | ||
} | ||
} | ||
visited[x][y] = false; // backtrack | ||
|
||
return false; // 4방향 다 실패하면 false | ||
|
||
} | ||
} |
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.
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.
visited와 dir 모두 전역에서 사용되는 변수들이라 클래스 변수 혹은 파라미터를 통해 전달하는 방식으로 사용해도 괜찮을것 같네요!