Skip to content

Commit 849ece4

Browse files
authored
Merge pull request #564 from TonyKim9401/main
[Tony] WEEK 12 Solutions
2 parents fa89896 + eda7ce0 commit 849ece4

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

merge-intervals/TonyKim9401.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// TC: O(n log n)
2+
// It takes n log n to sort array, visit all elements in O(n) time, total O(n log n)
3+
// SC: O(n)
4+
// Needs max O(n) space to save intervals
5+
class Solution {
6+
public int[][] merge(int[][] intervals) {
7+
int n = intervals.length;
8+
if (n == 1) return intervals;
9+
10+
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
11+
List<int[]> output = new ArrayList<>();
12+
output.add(intervals[0]);
13+
14+
int[] currentInterval = intervals[0];
15+
16+
for (int[] interval : intervals) {
17+
int currentEnd = currentInterval[1];
18+
int nextStart = interval[0];
19+
int nextEnd = interval[1];
20+
if (currentEnd >= nextStart) {
21+
currentInterval[1] = Math.max(currentEnd, nextEnd);
22+
} else {
23+
currentInterval = interval;
24+
output.add(currentInterval);
25+
}
26+
}
27+
28+
return output.toArray(new int[output.size()][]);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// TC: O(n + m)
2+
// n = the number of nodes, m = the number of edges
3+
// SC: O(n + m)
4+
// n, m are the each size of the 2 demension array 'edges'
5+
public class Solution {
6+
public int countComponents(int n, int[][] edges) {
7+
List<List<Integer>> graph = new ArrayList<>();
8+
9+
for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
10+
11+
for (int[] edge : edges) {
12+
graph.get(edge[0]).add(edge[1]);
13+
graph.get(edge[1]).add(edge[0]);
14+
}
15+
16+
boolean[] visit = new boolean[n];
17+
int count = 0;
18+
19+
for (int i = 0; i < n; i++) {
20+
if (!visit[i]) {
21+
count += 1;
22+
dfs(i, graph, visit);
23+
}
24+
}
25+
return count;
26+
}
27+
28+
private void dfs(int node, List<List<Integer>> graph, boolean[] visit) {
29+
visit[node] = true;
30+
for (int neighbor : graph.get(node)) {
31+
if (!visit[neighbor]) dfs(neighbor, graph, visit);
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n)
2+
// Visit all elements in the worst case
3+
// SC: O(1)
4+
// Keep using ready assigned variables only
5+
class Solution {
6+
public ListNode removeNthFromEnd(ListNode head, int n) {
7+
ListNode output = new ListNode(0, head);
8+
ListNode dummy = output;
9+
10+
for (int i = 0; i < n; i++) head = head.next;
11+
12+
while (head != null) {
13+
head = head.next;
14+
dummy = dummy.next;
15+
}
16+
17+
dummy.next = dummy.next.next;
18+
19+
return output.next;
20+
}
21+
}

same-tree/TonyKim9401.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n)
2+
// retreive all given nodes
3+
// SC: O(1)
4+
// doesn't require additional space
5+
class Solution {
6+
public boolean isSameTree(TreeNode p, TreeNode q) {
7+
if (p == null || q == null) {
8+
if (p == null && q == null) return true;
9+
return false;
10+
}
11+
12+
if (p.val != q.val) return false;
13+
14+
return isSameTree(p.left, q.left) &&
15+
isSameTree(p.right, q.right);
16+
}
17+
}

0 commit comments

Comments
 (0)