Skip to content

Commit e191914

Browse files
authored
Merge pull request #1365 from Yn3-3xh/main
[Yn3-3xh] WEEK 04 solutions
2 parents 4aee128 + cd835e1 commit e191914

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

coin-change/Yn3-3xh.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
[문제풀이]
3+
- 배열에 주어진 수를 더해서 amount 만들기
4+
- 배열은 중복 가능
5+
- amount를 만들 수 없으면 -1
6+
- amount를 만든 동전의 최소 개수 구하기
7+
- DP
8+
time: O(N M), space: O(N)
9+
10+
[회고]
11+
DP로 풀면 되지 않을까 라는 짐작은 가는데,
12+
아직 풀이방법이 부족하다..
13+
해결방법을 보고 겨우 이해는 했다..
14+
*/
15+
class Solution {
16+
public int coinChange(int[] coins, int amount) {
17+
int[] dp = new int[amount + 1];
18+
Arrays.fill(dp, amount + 1);
19+
dp[0] = 0;
20+
21+
for (int coin : coins) {
22+
for (int i = coin; i <= amount; i++) {
23+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
24+
}
25+
}
26+
return dp[amount] > amount ? -1 : dp[amount];
27+
}
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
[문제풀이]
3+
- 왼쪽의 수 보다 오른쪽의 수가 작은 순간을 찾자.
4+
- 가장 왼쪽의 수 보다 가장 오른쪽의 수가 크면 회전이 일어나지 않은 상태이므로, 첫번째가 최솟값이다.
5+
- 풀이1
6+
time: O(N), space: O(1);
7+
class Solution {
8+
public int findMin(int[] nums) {
9+
if (nums.length == 1) {
10+
return nums[0];
11+
}
12+
13+
for (int i = 1; i < nums.length; i++) {
14+
if (nums[i - 1] > nums[i]) {
15+
return nums[i];
16+
}
17+
}
18+
return nums[0];
19+
}
20+
}
21+
- 풀이2 <이진탐색>
22+
time: O(log N), space: O(1)
23+
24+
[회고]
25+
이진탐색으로 푸는 능력이 부족한 것 같다.
26+
*/
27+
28+
class Solution {
29+
public int findMin(int[] nums) {
30+
int left = 0;
31+
int right = nums.length - 1;
32+
33+
if (nums[left] < nums[right]) {
34+
return nums[left];
35+
}
36+
37+
while (left < right) {
38+
int mid = (left + right) / 2;
39+
40+
if (nums[mid] <= nums[right]) {
41+
right = mid;
42+
} else {
43+
left = mid + 1;
44+
}
45+
}
46+
return nums[left];
47+
}
48+
}
49+
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
[문제풀이]
3+
- 최대 depth 구하기
4+
- left or right의 null일 때 depth의 max 구하기
5+
- DFS 1
6+
time: O(N), space: O(N)
7+
class Solution {
8+
public int maxDepth(TreeNode root) {
9+
return nextNode(root, 0);
10+
}
11+
12+
private int nextNode(TreeNode node, int depth) {
13+
if (node == null) {
14+
return depth;
15+
}
16+
17+
int leftDepth = nextNode(node.left, depth + 1);
18+
int rightDepth = nextNode(node.right, depth + 1);
19+
return Math.max(leftDepth, rightDepth);
20+
}
21+
}
22+
- DFS 2
23+
time: O(N), space: O(N)
24+
25+
[회고]
26+
이전에 풀었던 Merget Two Sorted Lists 문제에서 주어진 메서드를 재사용할 수 있겠다는 생각으로 풀 수 있었다!
27+
*/
28+
29+
/**
30+
* Definition for a binary tree node.
31+
* public class TreeNode {
32+
* int val;
33+
* TreeNode left;
34+
* TreeNode right;
35+
* TreeNode() {}
36+
* TreeNode(int val) { this.val = val; }
37+
* TreeNode(int val, TreeNode left, TreeNode right) {
38+
* this.val = val;
39+
* this.left = left;
40+
* this.right = right;
41+
* }
42+
* }
43+
*/
44+
class Solution {
45+
public int maxDepth(TreeNode root) {
46+
if (root == null) {
47+
return 0;
48+
}
49+
50+
int leftDepth = maxDepth(root.left);
51+
int rightDepth = maxDepth(root.right);
52+
return Math.max(leftDepth, rightDepth) + 1;
53+
}
54+
}
55+

merge-two-sorted-lists/Yn3-3xh.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
[문제풀이]
3+
time: O(N + M), space: O(N + M)
4+
- list1과 list2를 비교해서 list2의 수가 작으면 list1을 list2로 교체하자.
5+
- 이때 list1은 list2의 자리에 들어갈 것이다.
6+
7+
[회고]
8+
주어진 메서드를 dfs로 활용할 수도 있구나!
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* public class ListNode {
14+
* int val;
15+
* ListNode next;
16+
* ListNode() {}
17+
* ListNode(int val) { this.val = val; }
18+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
19+
* }
20+
*/
21+
class Solution {
22+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
23+
if (list1 == null || list2 == null) {
24+
return list1 == null ? list2 : list1;
25+
}
26+
27+
if (list1.val > list2.val) {
28+
ListNode temp = list1;
29+
list1 = list2;
30+
list2 = temp;
31+
}
32+
33+
list1.next = mergeTwoLists(list1.next, list2);
34+
return list1;
35+
}
36+
}
37+

word-search/Yn3-3xh.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
[문제풀이]
3+
- 이웃되는 알파벳이 이어져야 한다.
4+
- 각 알파벳은 한번만 호출되어야 한다.
5+
- DFS
6+
time: O(N M 4^L), space: O(L)
7+
8+
[회고]
9+
BFS로 풀면 되지 않을까 했지만, 최단경로를 찾는 것이 아니니 안 어울릴 것 같다.
10+
DFS로 풀자는 방법은 맞았지만, 풀이과정이 어려웠다.
11+
다시 풀어보면 좋을 것 같다.
12+
*/
13+
14+
class Solution {
15+
public boolean exist(char[][] board, String word) {
16+
int column = board.length;
17+
int row = board[0].length;
18+
boolean[][] visited = new boolean[column][row];
19+
20+
for (int i = 0; i < column; i++) {
21+
for (int j = 0; j < row; j++) {
22+
if (board[i][j] == word.charAt(0)) {
23+
if (dfs(board, word, visited, i, j, 0)) {
24+
return true;
25+
}
26+
}
27+
}
28+
}
29+
return false;
30+
}
31+
32+
private boolean dfs(char[][] board, String word, boolean[][] visited, int i, int j, int wordIndex) {
33+
if (wordIndex == word.length()) {
34+
return true;
35+
}
36+
37+
if (isVisited(board, word, visited, i, j, wordIndex)) {
38+
return false;
39+
}
40+
41+
visited[i][j] = true;
42+
if (dfs(board, word, visited, i + 1, j, wordIndex + 1) ||
43+
dfs(board, word, visited, i - 1, j, wordIndex + 1) ||
44+
dfs(board, word, visited, i, j + 1, wordIndex + 1) ||
45+
dfs(board, word, visited, i, j - 1, wordIndex + 1)
46+
) {
47+
return true;
48+
}
49+
50+
visited[i][j] = false;
51+
return false;
52+
}
53+
54+
private boolean isVisited(char[][] board, String word, boolean[][] visited, int i, int j, int wordIndex) {
55+
return i < 0 ||
56+
j < 0 ||
57+
i >= board.length ||
58+
j >= board[0].length ||
59+
visited[i][j] ||
60+
board[i][j] != word.charAt(wordIndex);
61+
}
62+
}
63+

0 commit comments

Comments
 (0)