Skip to content

Commit 367b85d

Browse files
authored
Merge pull request #1339 from Tessa1217/main
[Tessa1217] Week 04 Solutions
2 parents 80611a9 + 2af5961 commit 367b85d

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

coin-change/Tessa1217.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 정수 배열 coins가 주어졌을 때 amount를 만들 기 위해 최소한의 동전 개수를 반환하세요.
3+
만약 동적으로 amount 조합을 만들어낼 수 없다면 -1을 리턴하세요.
4+
*/
5+
import java.util.Arrays;
6+
7+
class Solution {
8+
9+
// 시간복잡도: O(n * amount), 공간복잡도: O(amount)
10+
public int coinChange(int[] coins, int amount) {
11+
int[] coinCnt = new int[amount + 1];
12+
// coins[i]의 최댓값이 2^31 - 1 이므로 최댓값 설정
13+
Arrays.fill(coinCnt, Integer.MAX_VALUE - 1);
14+
coinCnt[0] = 0;
15+
for (int i = 0; i < coins.length; i++) {
16+
for (int j = coins[i]; j < amount + 1; j++) {
17+
coinCnt[j] = Math.min(coinCnt[j], coinCnt[j - coins[i]] + 1);
18+
}
19+
}
20+
if (coinCnt[amount] == Integer.MAX_VALUE - 1) {
21+
return -1;
22+
}
23+
return coinCnt[amount];
24+
}
25+
}
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 길이가 n인 오름차순으로 정렬된 숫자 배열이 1 ~ n번 회전했다. 회전된 배열에서 가장 작은 수를 찾아서 반환하시오.
3+
*/
4+
class Solution {
5+
6+
// 이진 탐색: 시간복잡도: O(log n)
7+
public int findMin(int[] nums) {
8+
int left = 0;
9+
int right = nums.length - 1;
10+
while (left < right) {
11+
int mid = (left + right) / 2;
12+
if (nums[mid] > nums[right]) {
13+
left = mid + 1;
14+
} else {
15+
right = mid;
16+
}
17+
}
18+
return nums[left];
19+
}
20+
21+
22+
// 1차는 단순하게 앞뒤 배열 요소 비교로 풀어봄: O(n^2)
23+
// Submission은 되었지만 시간 복잡도 기준을 못 맞춘 것 같아서 이진 탐색으로 재풀이 진행
24+
// public int findMin(int[] nums) {
25+
// int rotate = 0;
26+
// for (int i = 0; i < nums.length - 1; i++) {
27+
// int idx = (i + rotate) < nums.length ? (i + rotate) : (i + rotate) - nums.length;
28+
// int nextIdx = idx + 1 < nums.length ? idx + 1 : nums.length - (idx + 1);
29+
// if (nums[idx] > nums[nextIdx]) {
30+
// rotate++;
31+
// i = -1;
32+
// }
33+
// }
34+
// return nums[rotate];
35+
// }
36+
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
/**
17+
* 이진 트리의 루트가 주어질 때 최대 깊이를 구하세요.
18+
*/
19+
class Solution {
20+
21+
int maxDepth = 0;
22+
23+
// 시간 복잡도: O(n)
24+
public int maxDepth(TreeNode root) {
25+
depthChk(root, 0);
26+
return maxDepth;
27+
}
28+
29+
// 재귀로 풀이 진행
30+
public void depthChk(TreeNode node, int depth) {
31+
// 탐색할 노드 없을 경우
32+
if (node == null) {
33+
maxDepth = Math.max(depth, maxDepth);
34+
return;
35+
}
36+
// 트리의 좌우 탐색
37+
depthChk(node.left, depth + 1);
38+
depthChk(node.right, depth + 1);
39+
}
40+
}
41+

merge-two-sorted-lists/Tessa1217.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
/**
12+
정렬된 링크드 리스트 list1과 list2가 주어질 때 두 리스트를 하나의 정렬된 리스트로 반환하도록 구현하세요.
13+
*/
14+
class Solution {
15+
16+
// 시간 복잡도: O(l1 + l2), l1과 l2는 리스트 각각의 길이
17+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
18+
if (list1 == null || list2 == null) {
19+
return list1 == null ? list2 : list1;
20+
}
21+
if (list1.val < list2.val) {
22+
list1.next = mergeTwoLists(list1.next, list2);
23+
return list1;
24+
} else {
25+
list2.next = mergeTwoLists(list2.next, list1);
26+
return list2;
27+
}
28+
}
29+
}
30+

word-search/Tessa1217.java

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* character들로 이루어진 m*n 행렬 board와 word 문자열이 주어질 때 해당 문자열이 행렬 내에 존재할 수 있는지
3+
* 여부를 불린형으로 반환하세요. (상하좌우로 연결, 셀은 한번만 사용 가능)
4+
*/
5+
class Solution {
6+
7+
int[] dx = {-1, 1, 0, 0};
8+
9+
int[] dy = {0, 0, -1, 1};
10+
11+
int rows, cols;
12+
13+
// 시간 복잡도: O(M * N * 4^L)
14+
public boolean exist(char[][] board, String word) {
15+
rows = board.length;
16+
cols = board[0].length;
17+
18+
// 셀 사용 여부를 나타내는 방문 배열
19+
boolean[][] visited = new boolean[rows][cols];
20+
21+
for (int i = 0; i < rows; i++) {
22+
for (int j = 0; j < cols; j++) {
23+
// 글자의 시작점부터 DFS
24+
if (board[i][j] == word.charAt(0)) {
25+
if (dfs(i, j, 0, visited, board, word)) {
26+
return true;
27+
}
28+
}
29+
}
30+
}
31+
return false;
32+
}
33+
34+
// x, y가 board 벗어나지 않는지
35+
private boolean isRange(int x, int y) {
36+
return x >= 0 && x < rows && y >= 0 && y < cols;
37+
}
38+
39+
// DFS
40+
private boolean dfs(int x, int y, int idx, boolean[][] visited, char[][] board, String word) {
41+
42+
if (idx == word.length()) {
43+
return true;
44+
}
45+
46+
if (!isRange(x, y) || visited[x][y] || board[x][y] != word.charAt(idx)) {
47+
return false;
48+
}
49+
50+
visited[x][y] = true;
51+
52+
// 상하좌우
53+
for (int i = 0; i < 4; i++) {
54+
int nx = x + dx[i];
55+
int ny = y + dy[i];
56+
if (dfs(nx, ny, idx + 1, visited, board, word)) {
57+
return true;
58+
}
59+
}
60+
61+
visited[x][y] = false;
62+
63+
return false;
64+
65+
}
66+
}
67+

0 commit comments

Comments
 (0)