diff --git a/non-overlapping-intervals/forest000014.java b/non-overlapping-intervals/forest000014.java new file mode 100644 index 000000000..e47d4b933 --- /dev/null +++ b/non-overlapping-intervals/forest000014.java @@ -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; + } +} diff --git a/number-of-connected-components-in-an-undirected-graph/forest000014.java b/number-of-connected-components-in-an-undirected-graph/forest000014.java new file mode 100644 index 000000000..bca473a2a --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/forest000014.java @@ -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> 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> adj) { + visited[curr] = true; + + for (Integer next : adj.get(curr)) { + if (visited[next]) continue; + dfs(next, visited, adj); + } + } +} diff --git a/remove-nth-node-from-end-of-list/forest000014.java b/remove-nth-node-from-end-of-list/forest000014.java new file mode 100644 index 000000000..61c8a93ec --- /dev/null +++ b/remove-nth-node-from-end-of-list/forest000014.java @@ -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; + } +} diff --git a/same-tree/forest000014.java b/same-tree/forest000014.java new file mode 100644 index 000000000..170a2d3be --- /dev/null +++ b/same-tree/forest000014.java @@ -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); + } +}