-
-
Notifications
You must be signed in to change notification settings - Fork 305
[juhui-jeong] WEEK 11 solutions #2293
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
+92
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()][]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)으로 푸는 방법도 있으니 참고하시면 좋을 것 같습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 감사합니다! 제가 두 가지 풀이를 작성해 놓았는데, 상단의 풀이가 말씀하신 공식을 참고하여 재풀이를 한 내용입니다.
만약 해당 방법 외에 다른 방법도 있다면, 코멘트 부탁드립니다. 👍