Skip to content

Commit 081776d

Browse files
Merge pull request #1330 from YoungSeok-Choi/feature/week-4
[YoungSeok-Choi] Week 4 Solutions
2 parents 11e2561 + 771158f commit 081776d

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

coin-change/YoungSeok-Choi.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
public int coinChange(int[] coins, int amount) {
5+
int[] memo = new int[amount + 1];
6+
Arrays.fill(memo, amount + 1); // 도달 불가능한 초기값
7+
memo[0] = 0; // 0원을 만들기 위한 동전 수는 0개
8+
9+
for (int coin : coins) {
10+
for (int i = coin; i <= amount; i++) {
11+
memo[i] = Math.min(memo[i], memo[i - coin] + 1);
12+
}
13+
}
14+
15+
return memo[amount] > amount ? -1 : memo[amount];
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// NOTE: 문제에서 반드시 log n 복잡도의 알고리즘을 작성하라고 했는데.. 맞았다..
2+
// 이런걸 묻는건지... 다른 풀이 코드 보면서 복기가 필요함..
3+
class Solution {
4+
public int findMin(int[] nums) {
5+
int min = 9876521;
6+
7+
for(int i = 0; i < nums.length; i++) {
8+
min = Math.min(min, nums[i]);
9+
}
10+
11+
return min;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
// 시간복잡도 O(n)
5+
class Solution {
6+
public int depth = 0;
7+
public int maxDepth(TreeNode root) {
8+
9+
if(root == null) {
10+
return depth;
11+
}
12+
13+
Queue<TreeNode> q = new LinkedList<>();
14+
q.add(root);
15+
16+
while(!q.isEmpty()) {
17+
int size = q.size();
18+
depth++;
19+
20+
for(int i = 0; i < size; i++) {
21+
TreeNode p = q.poll();
22+
23+
if(p.right != null) {
24+
q.add(p.right);
25+
}
26+
27+
if(p.left != null) {
28+
q.add(p.left);
29+
}
30+
}
31+
}
32+
33+
return depth;
34+
}
35+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// 시간복잡도 O(N + M)
2+
class Solution {
3+
4+
public ListNode root = null;
5+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
6+
7+
while(list1 != null || list2 != null) {
8+
int v1 = 9999;
9+
int v2 = 9999;
10+
11+
if(list1 != null) {
12+
v1 = list1.val;
13+
}
14+
15+
if(list2 != null) {
16+
v2 = list2.val;
17+
}
18+
19+
if(v1 < v2) {
20+
addNode(v1);
21+
list1 = list1.next;
22+
} else {
23+
addNode(v2);
24+
list2 = list2.next;
25+
}
26+
}
27+
28+
return root;
29+
}
30+
31+
public void addNode (int val) {
32+
if(root == null) {
33+
root = new ListNode(val);
34+
return;
35+
}
36+
37+
ListNode now = root;
38+
while(now.next != null) {
39+
now = now.next;
40+
}
41+
42+
now.next = new ListNode(val);
43+
}
44+
}

word-search/YoungSeok-Choi.java

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// NOTE: Queue를 처음에 써서 탐색하며 꼬여버리는 문제가 있었다..
2+
// NOTE: 원본 문자의 index를 사용해서 해결.
3+
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
class Solution {
8+
9+
public boolean[][] visit;
10+
int w = 0;
11+
int h = 0;
12+
int[] dx = {1, 0, -1, 0};
13+
int[] dy = {0, 1, 0, -1};
14+
15+
public boolean exist(char[][] board, String word) {
16+
17+
char[] cArr = word.toCharArray();
18+
w = board.length;
19+
h = board[0].length;
20+
visit = new boolean[w][h];
21+
22+
for(int i = 0; i < board.length; i++) {
23+
for(int j = 0; j < board[0].length; j++) {
24+
if(cArr[0] == board[i][j]) {
25+
26+
if(dfs(board, word, i, j, 0)) {
27+
return true;
28+
}
29+
}
30+
}
31+
}
32+
33+
return false;
34+
}
35+
36+
public boolean dfs(char[][] b, String word, int x, int y, int idx) {
37+
if(idx == word.length()) return true;
38+
39+
if(x < 0 || x >= w || y < 0 || y >= h || b[x][y] != word.charAt(idx) || visit[x][y]) {
40+
return false;
41+
}
42+
43+
visit[x][y] = true;
44+
45+
for(int i = 0; i < 4; i++) {
46+
int nx = x + dx[i];
47+
int ny = y + dy[i];
48+
49+
if(dfs(b, word, nx, ny, idx + 1)) {
50+
return true;
51+
}
52+
}
53+
54+
visit[x][y] = false;
55+
return false;
56+
}
57+
58+
59+
60+
class WrongSolution {
61+
62+
public boolean[][] visit;
63+
Queue<Character> q = new LinkedList();
64+
int w = 0;
65+
int h = 0;
66+
int[] dx = {1, 0, -1, 0};
67+
int[] dy = {0, 1, 0, -1};
68+
69+
public boolean exist(char[][] board, String word) {
70+
71+
char[] cArr = word.toCharArray();
72+
w = board.length;
73+
h = board[0].length;
74+
visit = new boolean[w][h];
75+
76+
for(int i = 0; i < board.length; i++) {
77+
for(int j = 0; j < board[0].length; j++) {
78+
if(cArr[0] == board[i][j]) {
79+
q = new LinkedList();
80+
visit = new boolean[w][h];
81+
for(char c : word.toCharArray()) {
82+
q.add(c);
83+
}
84+
85+
86+
dfs(board, i, j);
87+
if(q.isEmpty()) {
88+
return true;
89+
}
90+
}
91+
}
92+
}
93+
94+
return false;
95+
}
96+
97+
public void dfs(char[][] b, int x, int y) {
98+
if(x < 0 || x >= w || y < 0 || y >= h || visit[x][y]) {
99+
return;
100+
}
101+
102+
if(q.isEmpty()) {
103+
return;
104+
}
105+
106+
if(b[x][y] != q.peek()) {
107+
return;
108+
}
109+
110+
q.poll();
111+
visit[x][y] = true;
112+
113+
for(int i = 0; i < 4; i++) {
114+
int nx = x + dx[i];
115+
int ny = y + dy[i];
116+
117+
dfs(b, nx, ny);
118+
}
119+
}
120+
}}

0 commit comments

Comments
 (0)