Skip to content

Commit 3c20455

Browse files
authored
Merge pull request #2304 from juhui-jeong/main
[juhui-jeong]WEEK 12 Solutions
2 parents 17646cf + d867d95 commit 3c20455

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* 끝나는 시간 오름차순 정렬
3+
*/
4+
class Solution {
5+
public int eraseOverlapIntervals(int[][] intervals) {
6+
Arrays.sort(intervals, (a,b) -> Integer.compare(a[1], b[1]));
7+
8+
int removeCntResult = 0;
9+
int end = intervals[0][1];
10+
11+
for (int i = 1; i< intervals.length; i++) {
12+
if (intervals[i][0] < end) {
13+
removeCntResult += 1;
14+
} else {
15+
end = intervals[i][1];
16+
}
17+
}
18+
return removeCntResult;
19+
}
20+
}
21+
22+
23+
/*
24+
처음 풀이
25+
시작 시간 기준 정렬
26+
class Solution {
27+
public int eraseOverlapIntervals(int[][] intervals) {
28+
Arrays.sort(intervals, (a,b) -> {
29+
if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
30+
return Integer.compare(a[1], b[1]);
31+
});
32+
33+
int removeCntResult = 0;
34+
int[] cur = intervals[0];
35+
36+
for (int i = 1; i< intervals.length; i++) {
37+
int [] next = intervals[i];
38+
39+
if (cur[1] > next[0]) {
40+
removeCntResult += 1;
41+
if (next[1] < cur[1]) {
42+
cur = next;
43+
}
44+
} else {
45+
cur = next;
46+
}
47+
}
48+
return removeCntResult;
49+
}
50+
}
51+
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(1)
4+
*/
5+
class Solution {
6+
public ListNode removeNthFromEnd(ListNode head, int n) {
7+
ListNode dummy = new ListNode(0);
8+
dummy.next = head;
9+
10+
ListNode fast = dummy;
11+
ListNode slow = dummy;
12+
13+
for (int i = 0; i <= n; i++) {
14+
fast = fast.next;
15+
}
16+
17+
while(fast != null) {
18+
fast = fast.next;
19+
slow = slow.next;
20+
}
21+
22+
slow.next = slow.next.next;
23+
24+
return dummy.next;
25+
}
26+
}
27+
28+
/*
29+
첫 번째 풀이
30+
class Solution {
31+
public ListNode removeNthFromEnd(ListNode head, int n) {
32+
List<ListNode> list = new ArrayList<>();
33+
34+
while(head != null) {
35+
list.add(head);
36+
head = head.next;
37+
}
38+
39+
list.remove(list.size()-n);
40+
if (list.isEmpty()) return null;
41+
42+
for (int i = 0; i < list.size()-1; i++) {
43+
list.get(i).next = list.get(i+1);
44+
}
45+
list.get(list.size()-1).next = null;
46+
return list.get(0);
47+
}
48+
}
49+
*/

same-tree/juhui-jeong.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* 시간복잡도: O(n)
3+
* 공간복잡도: O(h)
4+
*/
5+
class Solution {
6+
public boolean isSameTree(TreeNode p, TreeNode q) {
7+
if (p == null && q == null) return true;
8+
if (p == null || q == null) return false;
9+
if (p.val != q.val) return false;
10+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
11+
}
12+
}

0 commit comments

Comments
 (0)