Skip to content

Commit 3c35866

Browse files
authored
Merge pull request #1060 from forest000014/main
[forest000014] Week 12
2 parents bb30d9f + 51af125 commit 3c35866

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
# Time Complexity: O(nlogn)
3+
- 정렬 : O(nlogn)
4+
- for-loop : O(n)
5+
# Space Complexity: O(1)
6+
*/
7+
class Solution {
8+
public int eraseOverlapIntervals(int[][] intervals) {
9+
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
10+
11+
int cnt = 0;
12+
int prev_end = intervals[0][1];
13+
14+
for (int i = 1; i < intervals.length; i++) {
15+
if (prev_end > intervals[i][0]) {
16+
cnt++;
17+
} else {
18+
prev_end = intervals[i][1];
19+
}
20+
}
21+
22+
return cnt;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Comlexity: O(n + m)
4+
- m = edges.length
5+
*/
6+
class Solution {
7+
public int countComponents(int n, int[][] edges) {
8+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
9+
for (int i = 0; i < n; i++) {
10+
adj.add(new ArrayList<>());
11+
}
12+
for (int i = 0; i < edges.length; i++) {
13+
int a = edges[i][0];
14+
int b = edges[i][1];
15+
adj.get(a).add(b);
16+
adj.get(b).add(a);
17+
}
18+
19+
boolean[] visited = new boolean[n];
20+
int ans = 0;
21+
for (int i = 0; i < n; i++) {
22+
if (visited[i]) continue;
23+
dfs(i, visited, adj);
24+
ans++;
25+
}
26+
27+
return ans;
28+
}
29+
30+
public void dfs(int curr, boolean[] visited, ArrayList<ArrayList<Integer>> adj) {
31+
visited[curr] = true;
32+
33+
for (Integer next : adj.get(curr)) {
34+
if (visited[next]) continue;
35+
dfs(next, visited, adj);
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(1)
4+
*/
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* int val;
10+
* ListNode next;
11+
* ListNode() {}
12+
* ListNode(int val) { this.val = val; }
13+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
14+
* }
15+
*/
16+
class Solution {
17+
public ListNode removeNthFromEnd(ListNode head, int n) {
18+
ListNode curr = head;
19+
int cnt = 0;
20+
while (curr != null) {
21+
cnt++;
22+
curr = curr.next;
23+
}
24+
25+
if (cnt == n) { // 예외 처리
26+
return head.next;
27+
}
28+
29+
ListNode prev = null;
30+
curr = head;
31+
for (int i = 0; i < cnt - n; i++, prev = curr, curr = curr.next);
32+
prev.next = curr.next; // 뒤에서 n번째 노드 제거
33+
34+
return head;
35+
}
36+
}

same-tree/forest000014.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(1)
4+
*/
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* public class TreeNode {
9+
* int val;
10+
* TreeNode left;
11+
* TreeNode right;
12+
* TreeNode() {}
13+
* TreeNode(int val) { this.val = val; }
14+
* TreeNode(int val, TreeNode left, TreeNode right) {
15+
* this.val = val;
16+
* this.left = left;
17+
* this.right = right;
18+
* }
19+
* }
20+
*/
21+
class Solution {
22+
public boolean isSameTree(TreeNode p, TreeNode q) {
23+
if (p == null && q == null) {
24+
return true;
25+
} else if (p == null || q == null) {
26+
return false;
27+
}
28+
29+
if (p.val != q.val) {
30+
return false;
31+
}
32+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
33+
}
34+
}

0 commit comments

Comments
 (0)