Skip to content

Commit 0d88f1d

Browse files
authored
[JeonJe] WEEK 04 Solutions (#2742)
* merge two sorted lists solution * maximum depth of binary tree solution * find minimum in rotated sorted array solution * word search solution * coin change solution
1 parent 38e4c7b commit 0d88f1d

5 files changed

Lines changed: 161 additions & 0 deletions

File tree

coin-change/JeonJe.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
// TC: O(n * amount) (n = coins.length)
4+
// SC: O(amount)
5+
class Solution {
6+
7+
private static final int IMPOSSIBLE = Integer.MAX_VALUE;
8+
9+
private int[] memo;
10+
11+
public int coinChange(int[] coins, int amount) {
12+
memo = new int[amount + 1];
13+
Arrays.fill(memo, -1);
14+
15+
int result = minCoins(amount, coins);
16+
return result == IMPOSSIBLE ? -1 : result;
17+
}
18+
19+
private int minCoins(int target, int[] coins) {
20+
if (target == 0) return 0;
21+
if (target < 0) return IMPOSSIBLE;
22+
if (memo[target] != -1) return memo[target];
23+
24+
int best = IMPOSSIBLE;
25+
for (int coin : coins) {
26+
int sub = minCoins(target - coin, coins);
27+
if (sub != IMPOSSIBLE) {
28+
best = Math.min(best, sub + 1);
29+
}
30+
}
31+
return memo[target] = best;
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
3+
// TC: O(log n)
4+
// SC: O(1)
5+
class Solution {
6+
public int findMin(int[] nums) {
7+
8+
int lowIndex = 0;
9+
int highIndex = nums.length - 1;
10+
11+
while(lowIndex < highIndex) {
12+
int midIndex = (lowIndex + highIndex) / 2;
13+
14+
//오른 부분이 정렬 되지 않음 = 왼쪽 부분은 정렬됨
15+
if(nums[midIndex] > nums[highIndex]) {
16+
lowIndex = midIndex + 1;
17+
} else {
18+
//오른쪽 부분이 정렬 됨 = 왼쪽 부분을 봐야함
19+
highIndex = midIndex;
20+
}
21+
}
22+
return nums[lowIndex];
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.*;
2+
3+
// TC: O(n)
4+
// SC: O(h)
5+
class Solution {
6+
public int maxDepth(TreeNode root) {
7+
return calMaxDepth(root);
8+
}
9+
10+
// 현재까지 최대 깊이는 Math.max(왼쪽 서브노드 최대 깊이 , 오른쪽 서브노드 최대깊이) + 1
11+
private int calMaxDepth(TreeNode node) {
12+
if (node == null) return 0;
13+
return Math.max(calMaxDepth(node.left), calMaxDepth(node.right)) + 1;
14+
}
15+
}

merge-two-sorted-lists/JeonJe.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
// TC: O(n+m)
4+
// SC: O(n+m)
5+
class Solution {
6+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
7+
ListNode dummy = new ListNode(0);
8+
ListNode tail = dummy;
9+
10+
while (list1 != null && list2 != null) {
11+
if (list1.val > list2.val) {
12+
tail.next = new ListNode(list2.val);
13+
list2 = list2.next;
14+
} else {
15+
tail.next = new ListNode(list1.val);
16+
list1 = list1.next;
17+
}
18+
tail = tail.next;
19+
}
20+
21+
if (list1 != null) {
22+
tail.next = list1;
23+
}
24+
25+
if (list2 != null) {
26+
tail.next = list2;
27+
}
28+
29+
return dummy.next;
30+
}
31+
}

word-search/JeonJe.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.*;
2+
3+
// TC: O(m * n * 4^L)
4+
// SC: O(m * n)
5+
class Solution {
6+
7+
private char[][] board;
8+
private int m, n;
9+
private boolean[][] visited;
10+
private char[] word;
11+
private static final int[] dx = {-1, 1, 0, 0};
12+
private static final int[] dy = {0, 0, -1, 1};
13+
14+
15+
public boolean exist(char[][] board, String word) {
16+
this.board = board;
17+
this.word = word.toCharArray();
18+
this.n = board.length;
19+
this.m = board[0].length;
20+
this.visited = new boolean[n][m];
21+
22+
for (int i = 0; i < n; i++) {
23+
for (int j = 0; j < m; j++) {
24+
if (dfs(i, j, 0)) {
25+
return true;
26+
}
27+
}
28+
}
29+
return false;
30+
}
31+
32+
private boolean dfs(int x, int y, int wordIndex) {
33+
if (board[x][y] != word[wordIndex] || visited[x][y]) {
34+
return false;
35+
}
36+
if (wordIndex == word.length - 1) {
37+
return true;
38+
}
39+
40+
visited[x][y] = true;
41+
42+
for (int i = 0; i < 4; i++) {
43+
int nx = x + dx[i];
44+
int ny = y + dy[i];
45+
if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
46+
continue;
47+
}
48+
49+
if (board[nx][ny] == word[wordIndex + 1] && !visited[nx][ny]) {
50+
if (dfs(nx, ny, wordIndex + 1)) {
51+
return true;
52+
}
53+
}
54+
}
55+
visited[x][y] = false;
56+
return false;
57+
}
58+
}

0 commit comments

Comments
 (0)