Skip to content

Commit 698f669

Browse files
authored
Merge pull request #1362 from jinhyungrhee/week4
[jinhyungrhee] Week 04 Solutions
2 parents 8c393a3 + a03c011 commit 698f669

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

coin-change/jinhyungrhee.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
class Solution {
3+
int INF = 987654321;
4+
public int coinChange(int[] coins, int amount) {
5+
if (amount == 0) return 0;
6+
7+
int[] dp = new int[amount + 1];
8+
Arrays.fill(dp, INF);
9+
10+
dp[0] = 0;
11+
for (int coin : coins) {
12+
if (coin <= amount) dp[coin] = 1;
13+
}
14+
15+
for (int i = 1; i <= amount; i++) {
16+
for (int coin : coins) {
17+
if ((i - coin) >= 0) dp[i] = Math.min(dp[i], dp[i - coin] + 1);
18+
}
19+
}
20+
return (dp[amount] == INF) ? -1 : dp[amount];
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int left = 0, right = nums.length - 1;
4+
// 오름차순인 경우
5+
if (nums[left] < nums[right]) return nums[left];
6+
// 오름차순이 아닌 경우 이진탐색
7+
while (left < right) {
8+
int mid = (left + right) / 2;
9+
if (nums[mid] < nums[right]) {
10+
right = mid;
11+
} else {
12+
left = mid + 1;
13+
}
14+
}
15+
return nums[left];
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxVal;
3+
public int maxDepth(TreeNode root) {
4+
dfs(root, 0);
5+
return maxVal;
6+
}
7+
public void dfs(TreeNode node, int step) {
8+
9+
if (node == null) {
10+
if (maxVal < step) maxVal = step;
11+
return;
12+
}
13+
14+
dfs(node.left, step + 1);
15+
dfs(node.right, step + 1);
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
3+
4+
ListNode dummy = new ListNode(0);
5+
ListNode current = dummy; // ***실제로 노드를 이어나갈 포인터***
6+
7+
while (list1 != null && list2 != null) {
8+
if (list1.val < list2.val) {
9+
current.next = list1;
10+
list1 = list1.next;
11+
}
12+
else {
13+
current.next = list2;
14+
list2 = list2.next;
15+
}
16+
current = current.next;
17+
}
18+
19+
// 남아있는 노드들 이어붙이기
20+
if (list1 != null) {
21+
current.next = list1;
22+
}
23+
if (list2 != null) {
24+
current.next = list2;
25+
}
26+
27+
return dummy.next;
28+
}
29+
}

word-search/jinhyungrhee.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public boolean exist(char[][] board, String word) {
3+
4+
boolean[][] visited = new boolean[board.length][board[0].length];
5+
6+
for (int i = 0; i < board.length; i++) {
7+
for (int j = 0; j < board[0].length; j++) {
8+
if(dfs(board, word, visited, i, j, 0)) {
9+
return true;
10+
}
11+
}
12+
}
13+
return false;
14+
}
15+
16+
public boolean dfs(char[][] board, String word, boolean[][] visited, int x, int y, int index) {
17+
18+
// [성공] : 모든 글자 매칭 성공
19+
if (index == word.length()) return true;
20+
21+
// [실패] : 범위 초과, 방문함, 글자 불일치
22+
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length ||
23+
visited[x][y] || board[x][y] != word.charAt(index)) {
24+
return false;
25+
}
26+
27+
visited[x][y] = true;
28+
int[][] dir = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
29+
for (int i = 0; i < 4; i++) {
30+
int newX = x + dir[i][0], newY = y + dir[i][1];
31+
if (dfs(board, word, visited, newX, newY, index + 1)) {
32+
return true; // 찾았으면 바로 리턴
33+
}
34+
}
35+
visited[x][y] = false; // backtrack
36+
37+
return false; // 4방향 다 실패하면 false
38+
39+
}
40+
}

0 commit comments

Comments
 (0)