Skip to content

Commit 60f8363

Browse files
authored
Merge pull request #2488 from hyeri0903/main
[hyeri0903] WEEK 04 Solutions
2 parents e2b5640 + 62add02 commit 60f8363

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

coin-change/hyeri0903.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
int minLen = Integer.MAX_VALUE;
3+
public int coinChange(int[] coins, int amount) {
4+
/**
5+
1.문제: amount 를 만족시킬 수 있는 가장 적은 수의 coins
6+
2.조건
7+
- 모두 다른 값 integer로 구성된 배열
8+
- amount 를 만들 수 없는 경우 -1 return
9+
- coins.length 최소 1, 최대 12
10+
- 원소값 최소 = 1
11+
- amount 최소 = 0
12+
3.풀이
13+
- amount 를 만족시키는 최소 조합 길이를 구한다. dfs? -> TLE
14+
- dp
15+
*/
16+
17+
int n = coins.length;
18+
//최대 코인개수 = amount
19+
int[] dp = new int[amount + 1];
20+
Arrays.fill(dp, amount + 1);
21+
dp[0] = 0;
22+
23+
for(int i = 1; i <= amount; i++) {
24+
for(int coin : coins) {
25+
//코인 사용가능한 경우
26+
if(i - coin >= 0) {
27+
dp[i] = Math.min(dp[i], dp[i-coin] + 1);
28+
}
29+
}
30+
}
31+
//amount 못만든 경우
32+
if(dp[amount] > amount) {
33+
return -1;
34+
}
35+
return dp[amount];
36+
37+
// if(n != 0 && amount == 0) return 0;
38+
// Arrays.sort(coins);
39+
// dfs(n - 1, 0, coins, amount, 0);
40+
// if(minLen == Integer.MAX_VALUE) {
41+
// return -1;
42+
// }
43+
//return minLen;
44+
}
45+
46+
void dfs(int index, int total, int[] coins, int amount, int count) {
47+
if(total > amount) return;
48+
49+
if(total == amount) {
50+
minLen = Math.min(minLen, count);
51+
return;
52+
}
53+
54+
if(count >= minLen) return;
55+
56+
for(int i = index; i >=0; i--) {
57+
dfs(i, total + coins[i], coins, amount, count+1);
58+
}
59+
}
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
/**
4+
1.problem: 오름차순 정렬된 original으로부터 최소횟수만큼 n번 rotate한다. 이 배열에서 최솟값 구하기
5+
2.조건
6+
- 주어진 배열은 1 ~ n 번 rotated 되었다.
7+
- 배열의 원소는 모두 unique 한 값
8+
- n = numslength
9+
- O(log n)으로 풀이할것
10+
- n = 1 ~ 5000
11+
3.풀이
12+
- bruteforce: nums 돌다가 작아지는 부분이 생기면 거기가 rotate 한 횟수, time : O(n), space: O(1)
13+
- binary search: left, right 탐색
14+
15+
[1, 2, 3, 4, 5]
16+
[5, 1, 2, 3, 4] -> 1
17+
[4, 5, 1, 2, 3] -> 2
18+
[3, 4, 5, 1, 2] -> 3 : index 3 구간에서 숫자 작아짐
19+
*/
20+
int answer = nums[0];
21+
int n = nums.length;
22+
if(n == 1) return answer;
23+
24+
//sol 1)
25+
// for(int i = 0; i < n-1; i++) {
26+
// if(nums[i] > nums[i+1]) {
27+
// return nums[i+1];
28+
// }
29+
// }
30+
//return answer;
31+
32+
//sol 2)
33+
int left = 0;
34+
int right = n - 1;
35+
while(left < right) {
36+
int mid = (left + right) / 2;
37+
if(nums[mid] > nums[right]) {
38+
left = mid + 1;
39+
} else {
40+
right = mid;
41+
}
42+
}
43+
44+
return nums[left];
45+
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
public int maxDepth(TreeNode root) {
18+
/**
19+
문제: 바이너리트리의 maximum depth 구하기
20+
조건
21+
- 노드의 개수는 0 ~ 10^4
22+
- node val 은 -100 ~ 100
23+
풀이: dfs 로 depth 구하기
24+
- 주어진 트리는 binary tree
25+
- time complexity : O(n) , O(log n)
26+
- space complexity : O(h)
27+
*/
28+
29+
30+
if(root == null) return 0;
31+
32+
int height = dfs(root);
33+
return height;
34+
}
35+
36+
int dfs(TreeNode node) {
37+
if(node == null) return 0;
38+
39+
int left = dfs(node.left);
40+
int right = dfs(node.right);
41+
42+
return Math.max(left, right) + 1;
43+
}
44+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
14+
time: O(n + m)
15+
space: O(1)
16+
*/
17+
if(list1 == null) return list2;
18+
if(list2 == null) return list1;
19+
20+
ListNode res = new ListNode();
21+
ListNode tmp = res;
22+
23+
while(list1 != null && list2 != null) {
24+
if(list1.val > list2.val) {
25+
tmp.next = list2;
26+
list2 = list2.next;
27+
} else {
28+
tmp.next = list1;
29+
list1 = list1.next;
30+
}
31+
tmp = tmp.next;
32+
}
33+
if(list1 == null) {
34+
tmp.next = list2;
35+
} else {
36+
tmp.next = list1;
37+
}
38+
return res.next;
39+
}
40+
}

word-search/hyeri0903.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+
- backtracking (DFS)
5+
- board[i][j] == word[0] 부터 start
6+
- 상하좌우 탐색, 같은 칸 중복 탐색 x
7+
- 틀리면 backtracking
8+
9+
*/
10+
11+
for(int i = 0; i< board.length; i++) {
12+
for(int j = 0; j< board[i].length; j++) {
13+
if(dfs(i, j, 0, board, word)) {
14+
return true;
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
21+
boolean dfs(int i, int j, int index, char[][] board, String word) {
22+
//모두 다 찾은 경우
23+
if(index == word.length()) return true;
24+
25+
//out of bound check
26+
if(i < 0 || j < 0 || i >= board.length || j>= board[i].length || board[i][j] != word.charAt(index)) return false;
27+
28+
//방문처리
29+
char cur = board[i][j];
30+
board[i][j] = '#';
31+
32+
//4방향 탐색
33+
boolean found = dfs(i+1, j, index+1, board, word) || dfs(i-1, j, index+1, board, word) || dfs(i, j+1, index+1, board, word) || dfs(i, j-1, index+1, board, word);
34+
35+
//backtracking
36+
board[i][j] = cur;
37+
38+
return found;
39+
}
40+
}

0 commit comments

Comments
 (0)