Skip to content

Commit 3d09362

Browse files
authored
Merge pull request #2128 from YuuuuuuYu/main
[YuuuuuuYu] WEEK 04 solutions
2 parents 53098d9 + 39d9308 commit 3d09362

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(log n)
4+
*
5+
* Memory: 43.81MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: 이진 탐색
9+
*/
10+
class Solution {
11+
public int findMin(int[] nums) {
12+
int left = 0;
13+
int right = nums.length-1;
14+
15+
while (left < right) {
16+
int mid = left + (right-left)/2;
17+
if (nums[mid] <= nums[right]) {
18+
right = mid;
19+
} else {
20+
left = mid + 1;
21+
}
22+
}
23+
24+
return nums[left];
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 44.28MB
6+
* Space Complexity: O(h)
7+
* - h: 트리의 높이 or 최대 깊이
8+
*
9+
* Approach: 재귀를 이용한 DFS 접근법
10+
* - 루트부터 시작하여 재귀가 시작할 때마다 1로 계산
11+
* - 트리의 끝에 도달하면 0을 리턴하여 차례대로 남은 깊이를 계산
12+
*/
13+
class Solution {
14+
public int maxDepth(TreeNode root) {
15+
if (root == null)
16+
return 0;
17+
18+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(n)
4+
* - list1.length + list2.length
5+
*
6+
* Memory: 44.28MB
7+
* Space Complexity: O(1)
8+
*
9+
* Approach: 빈 node를 만들고 list1, list2의 현재 값을 비교하여 더 작은 값을 추가
10+
*
11+
*/
12+
class Solution {
13+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
14+
if (list1 == null) return list2;
15+
else if (list2 == null) return list1;
16+
17+
ListNode result = new ListNode();
18+
ListNode currentNode = result;
19+
while (list1 != null && list2 != null) {
20+
if (list1.val > list2.val) {
21+
currentNode.next = list2;
22+
list2 = list2.next;
23+
} else {
24+
currentNode.next = list1;
25+
list1 = list1.next;
26+
}
27+
28+
currentNode = currentNode.next;
29+
}
30+
31+
currentNode.next = list1 != null ? list1 : list2;
32+
return result.next;
33+
}
34+
}

word-search/YuuuuuuYu.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Runtime: 128ms
3+
* Time Complexity: O(m x n x 4^l)
4+
* - m: board의 행 길이
5+
* - n: board의 열 길이
6+
* - l: word의 길이
7+
*
8+
* Memory: 42.69MB
9+
* Space Complexity: O(l)
10+
*
11+
* Approach: DFS + 백트래킹
12+
* 1) board를 순회하며 word의 첫 글자와 일치하는 글자를 찾음
13+
* 2) 일치하는 글자를 찾으면 DFS를 통해 상하좌우로 다음 글자를 찾음
14+
* 3) 성능 최적화
15+
* - 백트래킹을 위해 방문한 글자는 임시로 다른 문자('#')로 변경 (비트마스크)
16+
* - charAt 캐싱
17+
*/
18+
class Solution {
19+
int[] dx = {-1, 1, 0, 0};
20+
int[] dy = {0, 0, -1, 1};
21+
22+
public boolean exist(char[][] board, String word) {
23+
int m = board.length;
24+
int n = board[0].length;
25+
char startChar = word.charAt(0);
26+
27+
for (int i=0; i<m; i++) {
28+
for (int j=0; j<n; j++) {
29+
if (board[i][j] == startChar) {
30+
if (dfs(board, i, j, 0, word)) {
31+
return true;
32+
}
33+
}
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
boolean dfs(char[][] board, int x, int y, int index, String word) {
41+
if (index == word.length()-1) {
42+
return true;
43+
}
44+
45+
char temp = board[x][y];
46+
char nextChar = word.charAt(index+1);
47+
board[x][y] = ' ';
48+
49+
for (int i=0; i<4; i++) {
50+
int nx = x+dx[i];
51+
int ny = y+dy[i];
52+
53+
if (nx >= 0 && nx < board.length &&
54+
ny >= 0 && ny < board[0].length &&
55+
board[nx][ny] == nextChar) {
56+
57+
if (dfs(board, nx, ny, index+1, word)) {
58+
board[x][y] = temp;
59+
return true;
60+
}
61+
}
62+
}
63+
64+
board[x][y] = temp;
65+
return false;
66+
}
67+
}

0 commit comments

Comments
 (0)