-
-
Notifications
You must be signed in to change notification settings - Fork 361
[dahyeong-yun] WEEK 11 Solutions #2847
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * TC : O(n log n) | ||
| * - 처음에 Arrays.sort() 하는데 O(n log n) 소요. 이하 for loop는 O(n) | ||
| * SC : O(n) | ||
| * - intervals 배열의 크기 n 만큼 ArrayList 할당하므로 O(n) | ||
| */ | ||
| class Solution { | ||
| // 정확히 작업 정렬 하는 거랑 비슷한데 그걸 위상정렬이라 하던가. | ||
| // 각 0 번째 인덱스 값으로 정렬되어 있으면 | ||
| // 1번째 인덱스 값이 직전 interval[0] <= value <= interval[1] 인 경우에 합쳐진다. | ||
| // 겹치는 값이나 중복 값이 없다는 조건이 없다. 정렬도 보장되어 있지 않다. | ||
| // 하나씩 넣고 구간에 걸치는 경우에 합칠지 버릴지를 결정하면 될 듯 한데, 그걸 어떻게 n^2이 아닌 방식으로 하지 | ||
| public int[][] merge(int[][] intervals) { | ||
| List<int[]> answer = new ArrayList<>(); | ||
| Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | ||
|
|
||
| int currentStart = intervals[0][0]; | ||
| int currentEnd = intervals[0][1]; | ||
|
|
||
| for (int i = 1; i < intervals.length; i++) { | ||
| int[] pair = intervals[i]; | ||
|
|
||
| if (currentEnd >= pair[0]) { | ||
| currentEnd = Math.max(currentEnd, pair[1]); | ||
| } else { | ||
| answer.add(new int[] { currentStart, currentEnd }); | ||
| currentStart = pair[0]; | ||
| currentEnd = pair[1]; | ||
| } | ||
| } | ||
| answer.add(new int[] { currentStart, currentEnd }); | ||
|
|
||
| return answer.toArray(new int[0][]); | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석missing-number/dahyeong-yun.java/**
* TC : O(n)
* - 배열을 한 번 순회하면서 합을 구하기 때문에 n
* SC : O(1)
* - 별도 유의미한 공간 할당이 없음
*/
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int total = n * (n + 1) / 2;
int sum = 0;
for(int num : nums) sum+=num;
return total - sum;
}
}
📊 시간/공간 복잡도 분석
피드백: 배열을 한 번 순회하면서 합을 구하고, 수열의 전체 합과 비교해 누락된 값을 얻는다. 개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,15 @@ | ||
| /** | ||
| * TC : O(n) | ||
| * - 배열을 한 번 순회하면서 합을 구하기 때문에 n | ||
| * SC : O(1) | ||
| * - 별도 유의미한 공간 할당이 없음 | ||
| */ | ||
| class Solution { | ||
| public int missingNumber(int[] nums) { | ||
| int n = nums.length; | ||
| int total = n * (n + 1) / 2; | ||
| int sum = 0; | ||
| for(int num : nums) sum+=num; | ||
| return total - sum; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석reorder-list/dahyeong-yun.java/**
* TC : O(n)
* - 배열을 한 번 순회하면서 합을 구하기 때문에 O(n)
* SC : O(n)
* - ArrayList가 노드 전체의 길이 n만큼 할당되기 때문에 O(n)
*/
class Solution {
public void reorderList(ListNode head) {
List<ListNode> nodes = new ArrayList<>();
for (ListNode cur = head; cur != null; cur = cur.next)
nodes.add(cur);
int i = 0;
int j = nodes.size() - 1;
while (i < j) {
nodes.get(i).next = nodes.get(j);
i++;
if (i == j) break;
nodes.get(j).next = nodes.get(i);
j--;
}
nodes.get(j).next = null;
}
}
📊 시간/공간 복잡도 분석
피드백: 전체를 한 번 순회하며 노드를 배열에 저장하고 다시 양 끝에서 연결하기 때문에 시간 복잡도는 선형이고, 보조 배열로 O(n) 공간이 필요하다. 개선 제안: 필요 시 추가 공간을 줄이려면 중간에 리스트를 반으로 나눠 역순으로 연결하는 방식 등으로 O(1) 추가 공간 구현을 고려해볼 수 있습니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SC O(1) 로 풀리는 풀이도 있으니 참고하세요 ~ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * TC : O(n) | ||
| * - 배열을 한 번 순회하면서 합을 구하기 때문에 O(n) | ||
| * SC : O(n) | ||
| * - ArrayList가 노드 전체의 길이 n만큼 할당되기 때문에 O(n) | ||
| */ | ||
| class Solution { | ||
| public void reorderList(ListNode head) { | ||
| List<ListNode> nodes = new ArrayList<>(); | ||
| for (ListNode cur = head; cur != null; cur = cur.next) | ||
| nodes.add(cur); | ||
|
|
||
| int i = 0; | ||
| int j = nodes.size() - 1; | ||
|
|
||
| while (i < j) { | ||
| nodes.get(i).next = nodes.get(j); | ||
| i++; | ||
| if (i == j) break; | ||
| nodes.get(j).next = nodes.get(i); | ||
| j--; | ||
| } | ||
|
|
||
| nodes.get(j).next = null; | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
merge-intervals/dahyeong-yun.java
📊 시간/공간 복잡도 분석
피드백: 정렬으로 시작해 이후 순회에서 현재 구간과 다음 구간의 교집합 여부를 검사해 합치는 형태로, 전체 시간 복잡도는 O(n log n), 추가 공간은 리스트 저장 등으로 O(n)이다.
개선 제안: 현재 구현이 적절해 보입니다.