Skip to content

[forest000014] Week 12 #1060

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

Merged
merged 4 commits into from
Mar 1, 2025
Merged
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
24 changes: 24 additions & 0 deletions non-overlapping-intervals/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
# Time Complexity: O(nlogn)
- 정렬 : O(nlogn)
- for-loop : O(n)
# Space Complexity: O(1)
*/
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);

int cnt = 0;
int prev_end = intervals[0][1];

for (int i = 1; i < intervals.length; i++) {
if (prev_end > intervals[i][0]) {
cnt++;
} else {
prev_end = intervals[i][1];
}
}

return cnt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
# Time Complexity: O(n)
# Space Comlexity: O(n + m)
- m = edges.length
*/
class Solution {
public int countComponents(int n, int[][] edges) {
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < edges.length; i++) {
int a = edges[i][0];
int b = edges[i][1];
adj.get(a).add(b);
adj.get(b).add(a);
}

boolean[] visited = new boolean[n];
int ans = 0;
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
dfs(i, visited, adj);
ans++;
}

return ans;
}

public void dfs(int curr, boolean[] visited, ArrayList<ArrayList<Integer>> adj) {
visited[curr] = true;

for (Integer next : adj.get(curr)) {
if (visited[next]) continue;
dfs(next, visited, adj);
}
}
}
36 changes: 36 additions & 0 deletions remove-nth-node-from-end-of-list/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
# Time Complexity: O(n)
# Space Complexity: O(1)
*/

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode curr = head;
int cnt = 0;
while (curr != null) {
cnt++;
curr = curr.next;
}

if (cnt == n) { // 예외 처리
return head.next;
}

ListNode prev = null;
curr = head;
for (int i = 0; i < cnt - n; i++, prev = curr, curr = curr.next);
prev.next = curr.next; // 뒤에서 n번째 노드 제거

return head;
}
}
34 changes: 34 additions & 0 deletions same-tree/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
# Time Complexity: O(n)
# Space Complexity: O(1)
*/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} else if (p == null || q == null) {
return false;
}

if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}