Skip to content

Commit 4ab4ada

Browse files
authored
[Yiseull] WEEK 04 Solutions (#2755)
* feat: 21. Merge Two Sorted Lists solution (cherry picked from commit 89322c3554301c0859b5e61e08d20f6ab995533e) * feat: 104. Maximum Depth of Binary Tree solution (cherry picked from commit c08337da842878a78cbf56d19d4f4618d8e6bb2e) * feat: 153. Find Minimum in Rotated Sorted Array solution * feat: 322. Coin Change solution * feat: 79. Word Search solution * feat: 153. Find Minimum in Rotated Sorted Array solution2 * feat: 322. Coin Change solution2 * fix: 줄바꿈
1 parent f8f2c31 commit 4ab4ada

5 files changed

Lines changed: 138 additions & 0 deletions

File tree

coin-change/Yiseull.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
3+
private int answer = Integer.MAX_VALUE;
4+
5+
public int coinChange(int[] coins, int amount) {
6+
final int IMPOSSIBLE = amount + 1;
7+
8+
int[] dp = new int[amount + 1];
9+
Arrays.fill(dp, IMPOSSIBLE);
10+
dp[0] = 0;
11+
12+
for (int i = 1; i < amount + 1; i++) {
13+
for (int coin : coins) {
14+
if (coin <= i) {
15+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
16+
}
17+
}
18+
}
19+
20+
return dp[amount] == IMPOSSIBLE ? -1 : dp[amount];
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
return binarySearch(nums);
4+
}
5+
6+
// 공간복잡도: O(1), 시간복잡도: O(logn)
7+
private int binarySearch(int[] nums) {
8+
int left = 0, right = nums.length - 1;
9+
10+
while (left < right) {
11+
if (nums[left] <= nums[right]) return nums[left];
12+
13+
int mid = (left + right) / 2;
14+
15+
if (nums[right] < nums[mid]) left = mid + 1;
16+
else right = mid;
17+
}
18+
19+
return nums[left];
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
public int maxDepth(TreeNode root) {
19+
return dfs(root);
20+
}
21+
22+
private int dfs(TreeNode node) {
23+
if (node == null) {
24+
return 0;
25+
}
26+
27+
return 1 + Math.max(dfs(node.left), dfs(node.right));
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
ListNode head = new ListNode(-1);
14+
ListNode current = head;
15+
16+
while (list1 != null && list2 != null) {
17+
if (list1.val <= list2.val) {
18+
current.next = list1;
19+
list1 = list1.next;
20+
} else {
21+
current.next = list2;
22+
list2 = list2.next;
23+
}
24+
current = current.next;
25+
}
26+
27+
current.next = (list1 != null) ? list1 : list2;
28+
29+
return head.next;
30+
}
31+
}

word-search/Yiseull.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
3+
private final int[] dx = {-1, 1, 0, 0};
4+
private final int[] dy = {0, 0, -1, 1};
5+
6+
public boolean exist(char[][] board, String word) {
7+
for (int i = 0; i < board.length; i++) {
8+
for (int j = 0; j < board[0].length; j++) {
9+
if (board[i][j] == word.charAt(0) && dfs(board, word, i, j, 1)) {
10+
return true;
11+
}
12+
}
13+
}
14+
15+
return false;
16+
}
17+
18+
private boolean dfs(char[][] board, String word, int x, int y, int index) {
19+
if (index == word.length()) return true;
20+
21+
char tmp = board[x][y];
22+
board[x][y] = '0';
23+
24+
for (int i = 0; i < 4; i++) {
25+
int nextX = x + dx[i], nextY = y + dy[i];
26+
if (0 <= nextX && nextX < board.length && 0 <= nextY && nextY < board[0].length && board[nextX][nextY] == word.charAt(index)) {
27+
if(dfs(board, word, nextX, nextY, index + 1)) return true;
28+
}
29+
}
30+
31+
board[x][y] = tmp;
32+
33+
return false;
34+
}
35+
}

0 commit comments

Comments
 (0)