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
28 changes: 28 additions & 0 deletions merge-intervals/juhui-jeong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 시간 복잡도: O(nlogn)
* 공간 복잡도: O(n)
*/
class Solution {
public int[][] merge(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]);
});

List<int[]> merged = new ArrayList<>();
int[] cur = intervals[0];
merged.add(cur);

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

if(cur[1] >= next[0]) {
cur[1] = Math.max(cur[1], next[1]);
} else {
cur = next;
merged.add(cur);
}
}
return merged.toArray(new int[merged.size()][]);
}
}
31 changes: 31 additions & 0 deletions missing-number/juhui-jeong.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1부터 n까지의 합이 (n*(n+1))/2라는 공식을 이용하여 O(1)으로 푸는 방법도 있으니 참고하시면 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 감사합니다! 제가 두 가지 풀이를 작성해 놓았는데, 상단의 풀이가 말씀하신 공식을 참고하여 재풀이를 한 내용입니다.
만약 해당 방법 외에 다른 방법도 있다면, 코멘트 부탁드립니다. 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 시간 복잡도: O(n)
* 공간 복잡도: O(1)
*/
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
long expected = n * (n + 1) / 2;
long actual = 0;
for (int x : nums) {
actual += x;
}
return (int) (expected - actual);
}
}

/*
* 시간 복잡도: O(n log n)
* 공간 복잡도: O(1)
*/
class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (i != nums[i]) {
return i;
}
}
return nums.length;
}
}
33 changes: 33 additions & 0 deletions reorder-list/juhui-jeong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*

*/
class Solution {
public void reorderList(ListNode head) {
List<ListNode> list = new ArrayList<>();

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

int i = 0;
int j = list.size() -1;
while(i < j) {
ListNode left = list.get(i);
ListNode right = list.get(j);

left.next = right;
i++;

if (i == j) {
right.next = null;
break;
}

right.next = list.get(i);
j--;
}

list.get(i).next = null;
}
}