Skip to content

Commit 447de77

Browse files
authored
Merge pull request #2135 from se6816/main
[se6816] WEEK 04 solutions
2 parents 230f47c + 7fe3df4 commit 447de77

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed

coin-change/se6816.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
내림차순 정렬 후, DFS를 통한 탐색 방식
3+
coins 길이 -> N
4+
amount 값 -> M
5+
시간 복잡도 : O(N^M) -> 시간 초과
6+
공간 복잡도 : O(M)
7+
8+
*/
9+
class Solution2 {
10+
public int coinChange(int[] coins, int amount) {
11+
Arrays.sort(coins);
12+
for (int i = 0; i < coins.length / 2; i++) {
13+
int temp = coins[i];
14+
coins[i] = coins[coins.length-1-i];
15+
coins[coins.length-1-i] = temp;
16+
}
17+
return searchCoin(coins, 0, 0, amount, 0);
18+
}
19+
20+
public int searchCoin(int[] coins, int idx, int result, int target, int count) {
21+
if(result == target) {
22+
return count;
23+
}
24+
int result2 = Integer.MAX_VALUE;
25+
for(int i = idx; i < coins.length; i++) {
26+
int temp = result + coins[i];
27+
if(temp > target) {
28+
continue;
29+
}
30+
int r = searchCoin(coins, i, temp, target, count+1);
31+
if(r != -1) {
32+
result2 = Math.min(result2, r);
33+
}
34+
}
35+
36+
if(result2 == Integer.MAX_VALUE) {
37+
result2 = -1;
38+
}
39+
40+
return result2;
41+
}
42+
}
43+
44+
/**
45+
dp를 이용하여, 최저 값을 구하는 방식
46+
coins의 길이 -> N
47+
amount의 값 -> M
48+
시간 복잡도 : O(N*M)
49+
공간 복잡도 : O(M)
50+
*/
51+
class Solution {
52+
public int coinChange(int[] coins, int amount) {
53+
int[] dp = new int[amount + 1];
54+
Arrays.fill(dp, Integer.MAX_VALUE);
55+
dp[0] = 0;
56+
57+
for (int coin : coins) {
58+
for (int x = 0; x < amount; x++) {
59+
if(dp[x] == Integer.MAX_VALUE) {
60+
continue;
61+
}
62+
if((x + coin) > amount) {
63+
break;
64+
}
65+
66+
dp[x + coin] = Math.min(dp[x + coin], dp[x] + 1);
67+
68+
}
69+
}
70+
71+
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
72+
}
73+
74+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
for문 순회를 통해 최소값 찾기
3+
nums의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(1)
6+
*/
7+
class Solution {
8+
public int findMin(int[] nums) {
9+
int result = Integer.MAX_VALUE;
10+
for(int num : nums) {
11+
result = Math.min(result, num);
12+
}
13+
return result;
14+
}
15+
}
16+
17+
/**
18+
이분 탐색을 통해, 최소 요소를 구하는 방식
19+
nums의 길이 -> N
20+
시간 복잡도 : O(logN)
21+
공간 복잡도 : O(1)
22+
*/
23+
class Solution {
24+
public int findMin(int[] nums) {
25+
return nums[binarySearch(nums)];
26+
}
27+
28+
public int binarySearch(int[] nums) {
29+
int start = 0;
30+
int end = nums.length -1;
31+
while(start < end) {
32+
int mid = (start + end) / 2;
33+
if(nums[mid] < nums[end]) {
34+
end = mid;
35+
} else {
36+
start = mid + 1;
37+
}
38+
}
39+
return start;
40+
}
41+
}
42+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
DFS를 이용하여, 최대 깊이를 구하는 방식
3+
트리 노드의 개수 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(log N)
6+
*/
7+
class Solution {
8+
public int maxDepth(TreeNode root) {
9+
return calculateDepth(root, 0);
10+
}
11+
12+
public int calculateDepth(TreeNode node, int depth) {
13+
if(node == null) {
14+
return depth;
15+
}
16+
17+
return Math.max(calculateDepth(node.left, depth + 1), calculateDepth(node.right, depth + 1));
18+
}
19+
}

merge-two-sorted-lists/se6816.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
연결 리스트를 활용하여, 하나의 리스트를 만드는 방식
3+
시간 복잡도 : O(N+M)
4+
공간 복잡도 : O(1)
5+
*/
6+
class Solution {
7+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
8+
NodeList list=new NodeList();
9+
ListNode temp = null;
10+
while(list1 != null && list2 != null){
11+
if(list1.val >= list2.val){
12+
list2 = merge(list, list2);
13+
}else{
14+
list1 = merge(list, list1);
15+
}
16+
}
17+
while(list1 !=null){
18+
list1 = merge(list, list1);
19+
}
20+
while(list2 != null){
21+
list2 = merge(list, list2);
22+
}
23+
return list.head;
24+
}
25+
public class NodeList {
26+
ListNode head = null;
27+
ListNode tail = null;
28+
public NodeList(){
29+
}
30+
31+
public void add(ListNode tempNode){
32+
if(head == null){
33+
head = tempNode;
34+
tail = head;
35+
}else{
36+
tail.next = tempNode;
37+
tail = tail.next;
38+
}
39+
}
40+
41+
}
42+
43+
public ListNode merge(NodeList list, ListNode target) {
44+
ListNode tempNode = null;
45+
tempNode = target;
46+
list.add(target);
47+
target = target.next;
48+
tempNode.next = null;
49+
return target;
50+
}
51+
}

word-search/se6816.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
/**
3+
DFS를 통해 이중 배열에서 문자를 찾는 방식
4+
board의 길이 -> M
5+
board[i]의 길이 -> N
6+
시간 복잡도 : O(M*N)
7+
공간 복잡도 : O(M*N)
8+
*/
9+
class Solution {
10+
public static int[] moveX = {0, -1, 1, 0};
11+
public static int[] moveY = {-1, 0, 0, 1};
12+
public int N;
13+
public int M;
14+
public boolean exist(char[][] board, String word) {
15+
M = board.length;
16+
N = board[0].length;
17+
boolean result = false;
18+
char startCh = word.charAt(0);
19+
for(int i = 0; i < M; i++) {
20+
for(int j = 0; j < N; j++) {
21+
if(board[i][j] != startCh) {
22+
continue;
23+
}
24+
boolean[][] visited = new boolean[M][N];
25+
visited[i][j] = true;
26+
result = result || search(board, visited, i, j, 1, word);
27+
}
28+
}
29+
return result;
30+
}
31+
32+
public boolean search(char[][] board, boolean[][] visited, int x, int y, int len, String target) {
33+
if(len >= target.length()) {
34+
return true;
35+
}
36+
37+
boolean result = false;
38+
39+
for(int i = 0; i < 4; i++) {
40+
int tempX = x + moveX[i];
41+
int tempY = y + moveY[i];
42+
43+
if(outOfIndex(tempX, tempY)) {
44+
continue;
45+
}
46+
47+
if(visited[tempX][tempY]) {
48+
continue;
49+
}
50+
51+
if(board[tempX][tempY] != target.charAt(len)) {
52+
continue;
53+
}
54+
55+
visited[tempX][tempY] = true;
56+
result = search(board, visited, tempX, tempY, len + 1, target) || result;
57+
if(result) {
58+
break;
59+
}
60+
visited[tempX][tempY] = false;
61+
}
62+
63+
return result;
64+
}
65+
66+
public boolean outOfIndex(int x, int y){
67+
if(x < 0 || x >= M || y < 0 || y >= N) {
68+
return true;
69+
}
70+
71+
return false;
72+
}
73+
}

0 commit comments

Comments
 (0)