Skip to content

Commit be88a3d

Browse files
authored
Merge pull request #1096 from forest000014/main
[forest000014] Week 14
2 parents b4d306e + 08a6a7e commit be88a3d

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(n)
4+
- 재귀 호출 함수의 파라미터 root, depth, ans
5+
*/
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* public class TreeNode {
10+
* int val;
11+
* TreeNode left;
12+
* TreeNode right;
13+
* TreeNode() {}
14+
* TreeNode(int val) { this.val = val; }
15+
* TreeNode(int val, TreeNode left, TreeNode right) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
class Solution {
23+
public List<List<Integer>> levelOrder(TreeNode root) {
24+
List<List<Integer>> ans = new ArrayList<>();
25+
26+
dfs(root, 0, ans);
27+
28+
return ans;
29+
}
30+
31+
private void dfs(TreeNode root, int depth, List<List<Integer>> ans) {
32+
if (root == null) return;
33+
34+
if (ans.size() == depth) {
35+
ans.add(new ArrayList<>());
36+
}
37+
ans.get(depth).add(root.val);
38+
39+
dfs(root.left, depth + 1, ans);
40+
dfs(root.right, depth + 1, ans);
41+
}
42+
}

counting-bits/forest000014.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(1)
4+
*/
5+
class Solution {
6+
public int[] countBits(int n) {
7+
int[] ans = new int[n + 1];
8+
9+
for (int i = 0; i <= n; i++) {
10+
int x = i;
11+
while (x > 0) {
12+
ans[i] += (x & 1);
13+
x >>= 1;
14+
}
15+
}
16+
17+
return ans;
18+
}
19+
}

house-robber-ii/forest000014.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(n)
4+
*/
5+
class Solution {
6+
public int rob(int[] nums) {
7+
int n = nums.length;
8+
if (n == 1) return nums[0];
9+
10+
int[][][] dp = new int[n][2][2];
11+
12+
dp[0][0][0] = 0;
13+
dp[0][0][1] = 0;
14+
dp[0][1][0] = 0; //
15+
dp[0][1][1] = nums[0];
16+
for (int i = 1; i < n - 1; i++) {
17+
dp[i][0][0] = Math.max(dp[i - 1][0][0], dp[i - 1][1][0]);
18+
dp[i][0][1] = Math.max(dp[i - 1][0][1], dp[i - 1][1][1]);
19+
dp[i][1][0] = dp[i - 1][0][0] + nums[i];
20+
dp[i][1][1] = dp[i - 1][0][1] + nums[i];
21+
}
22+
23+
dp[n - 1][0][0] = Math.max(dp[n - 2][0][0], dp[n - 2][1][0]);
24+
dp[n - 1][0][1] = Math.max(dp[n - 2][0][1], dp[n - 2][1][1]);
25+
dp[n - 1][1][0] = dp[n - 2][0][0] + nums[n - 1];
26+
dp[n - 1][1][1] = -1;
27+
28+
return Math.max(Math.max(dp[n - 1][0][0], dp[n - 1][0][1]), dp[n - 1][1][0]);
29+
}
30+
}

meeting-rooms-ii/forest000014.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
# Time Complexity: O(nlogn)
3+
# Space Complexity: O(n)
4+
*/
5+
6+
class Solution {
7+
8+
private class Info {
9+
int kind; // start = 0, end = 1
10+
int time;
11+
12+
Info(int kind, int time) {
13+
this.kind = kind;
14+
this.time = time;
15+
}
16+
}
17+
public int minMeetingRooms(int[][] intervals) {
18+
int n = intervals.length;
19+
List<Info> infos = new ArrayList<>();
20+
for (int i = 0; i < n; i++) {
21+
infos.add(new Info(0, intervals[i][0]));
22+
infos.add(new Info(1, intervals[i][1]));
23+
}
24+
25+
Collections.sort(infos, (o1, o2) -> {
26+
if (o1.time == o2.time) {
27+
return Integer.compare(o2.kind, o1.kind);
28+
}
29+
return Integer.compare(o1.time, o2.time);
30+
});
31+
32+
int cnt = 0;
33+
int ans = 0;
34+
for (Info info : infos) {
35+
if (info.kind == 0) cnt++;
36+
else cnt--;
37+
ans = Math.max(ans, cnt);
38+
}
39+
40+
return ans;
41+
}
42+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(n)
4+
- 재귀 호출 파라미터 O(n) 사이즈 공간 필요
5+
*/
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* public class TreeNode {
10+
* int val;
11+
* TreeNode left;
12+
* TreeNode right;
13+
* TreeNode() {}
14+
* TreeNode(int val) { this.val = val; }
15+
* TreeNode(int val, TreeNode left, TreeNode right) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
class Solution {
23+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
24+
return dfs(root, subRoot);
25+
}
26+
27+
private boolean dfs(TreeNode root, TreeNode subRoot) {
28+
if (root == null && subRoot == null) return true;
29+
if (root == null || subRoot == null) return false;
30+
31+
if (root.val == subRoot.val) {
32+
if (compareTrees(root, subRoot)) return true;
33+
}
34+
35+
if (dfs(root.left, subRoot)) return true;
36+
if (dfs(root.right, subRoot)) return true;
37+
38+
return false;
39+
}
40+
41+
private boolean compareTrees(TreeNode root1, TreeNode root2) {
42+
if (root1 == null && root2 == null) return true;
43+
if (root1 == null || root2 == null) return false;
44+
if (root1.val != root2.val) return false;
45+
46+
return compareTrees(root1.left, root2.left) && compareTrees(root1.right, root2.right);
47+
}
48+
}

word-search-ii/forest000014.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
# Time Complexity: O(w + m * n * 4^10)
3+
- trie에 word 하나(최대 길이 10)를 삽입하는 데에는 O(10) = O(1) 이므로, trie 전체를 생성하는 데에는 O(w) (w는 words의 length)
4+
- dfs 탐색을 하면서, 모든 경로를 탐색 (최대 depth는 10)
5+
# Space Complexity: O(w)
6+
- trie를 생성하면, 최대 10글자 * w개 문자열 = O(10w) = O(w)
7+
*/
8+
class Solution {
9+
10+
private class Trie {
11+
char val;
12+
boolean ends;
13+
Map<Character, Trie> children;
14+
15+
Trie(char val) {
16+
this.val = val;
17+
this.children = new HashMap<>();
18+
}
19+
}
20+
public List<String> findWords(char[][] board, String[] words) {
21+
// Trie 생성 및 세팅
22+
Trie root = new Trie('.');
23+
for (String word : words) {
24+
Trie curr = root;
25+
for (int i = 0; i < word.length(); i++) {
26+
char ch = word.charAt(i);
27+
curr.children.putIfAbsent(ch, new Trie(ch));
28+
curr = curr.children.get(ch);
29+
}
30+
curr.ends = true;
31+
}
32+
33+
// trie와 dfs를 사용하여, 단어가 존재하는지 확인
34+
int m = board.length;
35+
int n = board[0].length;
36+
boolean[][] visited = new boolean[m][n];
37+
StringBuilder sb = new StringBuilder();
38+
Set<String> ans = new HashSet<>();
39+
for (int i = 0; i < m; i++) {
40+
for (int j = 0; j < n; j++) {
41+
if (!root.children.containsKey(board[i][j])) continue;
42+
43+
visited[i][j] = true;
44+
sb.append(board[i][j]);
45+
dfs(m, n, board, visited, i, j, root.children.get(board[i][j]), sb, ans); //
46+
sb.deleteCharAt(0);
47+
visited[i][j] = false;
48+
}
49+
}
50+
51+
return ans.stream().collect(Collectors.toList());
52+
}
53+
54+
private void dfs(int m, int n, char[][] board, boolean[][] visited, int r, int c, Trie curr, StringBuilder sb, Set<String> ans) {
55+
if (curr.ends) ans.add(sb.toString());
56+
57+
int[] dr = {-1, 0, 1, 0};
58+
int[] dc = {0, 1, 0, -1};
59+
60+
for (int i = 0; i < 4; i++) {
61+
int nr = r + dr[i];
62+
int nc = c + dc[i];
63+
if (nr < 0 || nr >= m || nc < 0 || nc >= n || visited[nr][nc]) continue;
64+
if (!curr.children.containsKey(board[nr][nc])) continue;
65+
66+
visited[nr][nc] = true;
67+
sb.append(board[nr][nc]);
68+
dfs(m, n, board, visited, nr, nc, curr.children.get(board[nr][nc]), sb, ans);
69+
sb.deleteCharAt(sb.length() - 1);
70+
visited[nr][nc] = false;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)