Skip to content

[sora0319] Week14 Solutions #1647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions binary-tree-level-order-traversal/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) return new ArrayList<>();

List<List<Integer>> output = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);

while (!q.isEmpty()) {
List<Integer> values = new ArrayList<>();
int size = q.size();

for (int i = 0; i < size; i++) {
TreeNode node = q.poll();
values.add(node.val);

if (node.left != null) q.offer(node.left);
if (node.right != null) q.offer(node.right);
}

output.add(values);
}

return output;
}
}
15 changes: 15 additions & 0 deletions counting-bits/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int[] countBits(int n) {
int[] bits = new int[n + 1];
int i = 1;

for (int num = 1; num <= n; num++) {
if (i << 1 == num) {
i = num;
}
bits[num] = 1 + bits[num - i];
}

return bits;
}
}
21 changes: 21 additions & 0 deletions house-robber-ii/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int rob(int[] nums) {
if (nums.length == 1) return nums[0];

return Math.max(dp(nums, 0, nums.length - 1),
dp(nums, 1, nums.length));
}

private int dp(int[] nums, int start, int end) {
int back = 0;
int curr = 0;

for (int i = start; i < end; i++) {
int temp = curr;
curr = Math.max(back + nums[i], curr);
back = temp;
}

return curr;
}
}
33 changes: 33 additions & 0 deletions meeting-rooms-ii/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Solution {
public int minMeetingRooms(int[][] intervals) {
int n = intervals.length;
int[] starts = new int[n];
int[] ends = new int[n];

for (int i = 0; i < n; i++) {
starts[i] = intervals[i][0];
ends[i] = intervals[i][1];
}

Arrays.sort(starts);
Arrays.sort(ends);

int maxCount = 0;
int count = 0;
int s = 0;
int e = 0;

while (s < n) {
if (starts[s] < ends[e]) {
count++;
maxCount = Math.max(maxCount, count);
s++;
} else {
count--;
e++;
}
}

return maxCount;
}
}