-
Notifications
You must be signed in to change notification settings - Fork 126
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
[river20s] Week 4 #808
Merged
Merged
[river20s] Week 4 #808
Changes from all commits
Commits
Show all changes
3 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 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,47 @@ | ||
/** | ||
* 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 { | ||
/* | ||
* [풀이] | ||
* 1) 두 리스트가 이미 정렬된 상태 → 맨 앞에서부터 둘 중 더 작은 노드를 결과 리스트 rs에 이어 붙인다. | ||
* 2) 하나의 리스트가 끝날 때까지 반복한다. → 남은 리스트의 노드는 그대로 rs에 붙인다. | ||
* 3) rs 헤드 노드의 next부터 반환한다. | ||
* [T.C] | ||
* 각 노드를 한 번씩 비교 → 연결(또는 연결만)하므로, | ||
* T.C = O(n+m) (n = list1의 길이, m = list2의 길이) | ||
* [S.C] | ||
* list1과 list2의 노드를 다시 연결하는 것이므로 추가적인 공간은 거의 필요하지 않으므로(rs의 head 정도?), | ||
* S.C = O(1) | ||
*/ | ||
|
||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
ListNode rs = new ListNode(0); | ||
ListNode rsNext = rs; | ||
// step 1 | ||
while (list1 != null && list2 != null) { | ||
if (list1.val <= list2.val) { | ||
rsNext.next = list1; | ||
list1 = list1.next; | ||
} | ||
else { | ||
rsNext.next = list2; | ||
list2 = list2.next; | ||
} | ||
rsNext = rsNext.next; | ||
} | ||
// step 2 | ||
if (list1 != null) rsNext.next = list1; | ||
if (list2 != null) rsNext.next = list2; | ||
// step3 | ||
return rs.next; | ||
} | ||
} | ||
|
This file contains 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 @@ | ||
import java.util.Arrays; | ||
class Solution { | ||
/* [풀이] | ||
* 1) 배열 nums을 오름차순 정렬한다. | ||
* 2) 배열 요소의 인덱스와 값을 비교한다. | ||
* 2-1) 인덱스와 값이 같다면 해당 값은 배열에 있다. | ||
* 2-2) 인덱스와 값이 다르다면, 해당 값은 배열에 없다. | ||
* 3) 다른 값의 인덱스를 반환한다. | ||
* [T.C] | ||
* 내장된 Arrays.sort()를 사용하면 시간 복잡도는 O(n log n)이 된다. | ||
* [S.C] | ||
* 최악의 경우 Arrays.sort()는 추가적으로 O(n) 만큼의 공간을 사용한다. | ||
*/ | ||
|
||
public int missingNumber(int[] nums) { | ||
// nums 오름차순 정렬 | ||
Arrays.sort(nums); | ||
// 인덱스와 요소 비교 | ||
for (int i = 0; i < nums.length; i++) { | ||
if (nums[i] != i) { | ||
return i; | ||
} | ||
} | ||
// 배열에 없는 값 반환 | ||
return nums.length | ||
} | ||
} | ||
|
Oops, something went wrong.
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.
가장 직관적이고 떠올리기 좋은 문제 풀이 방법이에요 :)
릿코드 문제에도 나와있는 것 처럼 O(n)의 시간복잡도로 푸는 방법도 있답니다.
O(n log n) 을 최적화 하여 O(n) 의 방법을 찾아보시면 어떨까요~!