Skip to content

Commit 8760af8

Browse files
authored
Merge pull request #2329 from juhui-jeong/main
[juhui-jeong] WEEK 14 Solutions
2 parents 902ed66 + 58283f9 commit 8760af8

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// BFS 구현
2+
// null 처리 잘 확인할 것...
3+
class Solution {
4+
public List<List<Integer>> levelOrder(TreeNode root) {
5+
List<List<Integer>> result = new ArrayList<>();
6+
if (root == null) return result;
7+
8+
Queue<TreeNode> queue = new LinkedList<>();
9+
queue.offer(root);
10+
11+
while(!queue.isEmpty()) {
12+
int size = queue.size();
13+
List<Integer> level = new ArrayList<>();
14+
15+
for (int i = 0; i < size; i++) {
16+
TreeNode cur = queue.poll();
17+
level.add(cur.val);
18+
19+
if (cur.left != null) queue.offer(cur.left);
20+
if (cur.right != null) queue.offer(cur.right);
21+
}
22+
result.add(level);
23+
}
24+
return result;
25+
}
26+
}

counting-bits/juhui-jeong.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
class Solution {
4+
public int[] countBits(int n) {
5+
ArrayList<Integer> list = new ArrayList<>();
6+
7+
for (int i = 0; i <= n; i++) {
8+
int result = i;
9+
int cnt = 0;
10+
11+
while(result > 0) {
12+
cnt += result%2;
13+
result /= 2;
14+
}
15+
16+
list.add(cnt);
17+
}
18+
int[] ans = new int[list.size()];
19+
for (int i = 0; i < list.size(); i++) {
20+
ans[i] = list.get(i);
21+
}
22+
return ans;
23+
}
24+
}

house-robber-ii/juhui-jeong.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
int n = nums.length;
4+
if (n == 1) return nums[0];
5+
if (n == 2) return Math.max(nums[0], nums[1]);
6+
7+
return Math.max(robRange(nums, 0, n - 2), robRange(nums, 1, n - 1));
8+
}
9+
10+
private int robRange(int[] nums, int start, int end) {
11+
int prev2 = 0;
12+
int prev1 = 0;
13+
14+
for (int i = start; i <= end; i++) {
15+
int cur = Math.max(prev1, prev2 + nums[i]);
16+
prev2 = prev1;
17+
prev1 = cur;
18+
}
19+
return prev1;
20+
}
21+
}

0 commit comments

Comments
 (0)