Skip to content
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
51 changes: 51 additions & 0 deletions non-overlapping-intervals/juhui-jeong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 끝나는 시간 오름차순 정렬
*/
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a,b) -> Integer.compare(a[1], b[1]));

int removeCntResult = 0;
int end = intervals[0][1];

for (int i = 1; i< intervals.length; i++) {
if (intervals[i][0] < end) {
removeCntResult += 1;
} else {
end = intervals[i][1];
}
}
return removeCntResult;
}
}


/*
처음 풀이
시작 시간 기준 정렬
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a,b) -> {
if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
return Integer.compare(a[1], b[1]);
});

int removeCntResult = 0;
int[] cur = intervals[0];

for (int i = 1; i< intervals.length; i++) {
int [] next = intervals[i];

if (cur[1] > next[0]) {
removeCntResult += 1;
if (next[1] < cur[1]) {
cur = next;
}
} else {
cur = next;
}
}
return removeCntResult;
}
}
*/
49 changes: 49 additions & 0 deletions remove-nth-node-from-end-of-list/juhui-jeong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
시간 복잡도: O(N)
공간 복잡도: O(1)
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;

ListNode fast = dummy;
ListNode slow = dummy;

for (int i = 0; i <= n; i++) {
fast = fast.next;
}

while(fast != null) {
fast = fast.next;
slow = slow.next;
}

slow.next = slow.next.next;

return dummy.next;
}
}

/*
첫 번째 풀이
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
List<ListNode> list = new ArrayList<>();

while(head != null) {
list.add(head);
head = head.next;
}

list.remove(list.size()-n);
if (list.isEmpty()) return null;

for (int i = 0; i < list.size()-1; i++) {
list.get(i).next = list.get(i+1);
}
list.get(list.size()-1).next = null;
return list.get(0);
}
}
*/
12 changes: 12 additions & 0 deletions same-tree/juhui-jeong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 시간복잡도: O(n)
* 공간복잡도: O(h)
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
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);
}
}