Skip to content

Commit 9108044

Browse files
committed
Week4 Solutions
1 parent a3ecb00 commit 9108044

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

coin-change/hoyeongkwak.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Time Complexity : O(n)
3+
Space Complexity : O(n)
4+
*/
5+
6+
class Solution {
7+
public int coinChange(int[] coins, int amount) {
8+
if (amount == 0) return 0;
9+
int[] dp = new int[amount + 1];
10+
Arrays.fill(dp, Integer.MAX_VALUE);
11+
dp[0] = 0;
12+
for (int coin : coins) {
13+
for (int i = coin; i <= amount; i++) {
14+
if (dp[i - coin] != Integer.MAX_VALUE) {
15+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
16+
}
17+
}
18+
}
19+
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Time Complexity : O(log n)
3+
Space Complexity : O(1)
4+
*/
5+
class Solution {
6+
public int findMin(int[] nums) {
7+
int left = 0;
8+
int right = nums.length - 1;
9+
10+
while (left < right) {
11+
int mid = left + (right - left) / 2;
12+
if (nums[mid] > nums[right]) {
13+
left = mid + 1;
14+
} else {
15+
right = mid;
16+
}
17+
}
18+
return nums[left];
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/*
17+
Time Complexity : O(n)
18+
Space Complexity : O(log n)
19+
*/
20+
class Solution {
21+
public int maxDepth(TreeNode root) {
22+
if (root == null) return 0;
23+
int maxLeft = maxDepth(root.left);
24+
int maxRight = maxDepth(root.right);
25+
return Math.max(maxLeft, maxRight) + 1;
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Time Complexity : O(m + n)
3+
Space Complexity : O(1)
4+
*/
5+
/**
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode() {}
11+
* ListNode(int val) { this.val = val; }
12+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
13+
* }
14+
*/
15+
class Solution {
16+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
17+
ListNode tempNode = new ListNode(0);
18+
ListNode current = tempNode;
19+
20+
while (list1 != null && list2 != null) {
21+
if (list1.val <= list2.val) {
22+
current.next = list1;
23+
list1 = list1.next;
24+
} else {
25+
current.next = list2;
26+
list2 = list2.next;
27+
}
28+
current = current.next;
29+
}
30+
31+
current.next = (list1 != null) ? list1 : list2;
32+
return tempNode.next;
33+
// if (list1 == null) {
34+
// return list2;
35+
// }
36+
// if (list2 == null) {
37+
// return list1;
38+
// }
39+
// if (list1.val < list2.val) {
40+
// list1.next = mergeTwoLists(list1.next, list2);
41+
// return list1;
42+
// } else {
43+
// list2.next = mergeTwoLists(list1, list2.next);
44+
// return list2;
45+
// }
46+
}
47+
}

word-search/hoyeongkwak.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
n : 셀 수
3+
L : 단어 길이
4+
Time Complexity : O(n * 4^L)
5+
Space Complexity : O(L)
6+
*/
7+
8+
class Solution {
9+
public boolean exist(char[][] board, String word) {
10+
if (board == null || board.length == 0 || board[0].length == 0) return false;
11+
int rows = board.length;
12+
int cols = board[0].length;
13+
boolean[][] visited = new boolean[rows][cols];
14+
15+
for (int i = 0; i < rows; i++) {
16+
for (int j = 0; j < cols; j++) {
17+
if (backtrack(board, word, i, j, 0, visited)) {
18+
return true;
19+
}
20+
}
21+
}
22+
return false;
23+
}
24+
25+
private boolean backtrack(char[][] board, String word, int row, int col, int index, boolean[][] visited) {
26+
if (index == word.length()) return true;
27+
if (row < 0 || row >= board.length || col < 0 || col >= board[0].length || visited[row][col] || board[row][col] != word.charAt(index)) {
28+
return false;
29+
}
30+
visited[row][col] = true;
31+
32+
int[] dx = {-1, 1, 0, 0};
33+
int[] dy = {0, 0, -1, 1};
34+
35+
for (int i = 0; i < 4; i++) {
36+
int newRow = row + dx[i];
37+
int newCol = col + dy[i];
38+
39+
if (backtrack(board, word, newRow, newCol, index + 1, visited)) {
40+
visited[row][col] = false;
41+
return true;
42+
}
43+
}
44+
visited[row][col] = false;
45+
return false;
46+
}
47+
}

0 commit comments

Comments
 (0)