-
-
Notifications
You must be signed in to change notification settings - Fork 195
[박종훈] 13주차 답안 제출 #198
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
Merged
[박종훈] 13주차 답안 제출 #198
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e494ff9
week13 mission non-overlapping-intervals
dev-jonghoonpark f39de4c
week13 mission meeting-rooms-ii
dev-jonghoonpark 202dbd7
week13 mission rotate-image
dev-jonghoonpark b8d42fd
week13 mission merge-intervals
dev-jonghoonpark 418c176
week13 mission insert-interval
dev-jonghoonpark d1b731f
Merge branch 'DaleStudy:main' into main
dev-jonghoonpark 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,101 @@ | ||
- 문제: https://leetcode.com/problems/insert-interval/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/02/14/leetcode-57 | ||
|
||
## Merge Intervals 문제 답안 응용하기 | ||
|
||
바로 전에 풀었던 [Merge Intervals](https://algorithm.jonghoonpark.com/2024/07/23/leetcode-56)를 그대로 가져다 쓸 수 있을 것 같아서 해보았더니 통과 된다. | ||
|
||
```java | ||
public int[][] insert(int[][] intervals, int[] newInterval) { | ||
int[][] newIntervals = Arrays.copyOf(intervals, intervals.length + 1); | ||
newIntervals[intervals.length] = newInterval; | ||
return merge(newIntervals); | ||
} | ||
|
||
public int[][] merge(int[][] intervals) { | ||
Arrays.sort(intervals, Comparator.comparingInt(o -> o[0])); | ||
|
||
Deque<int[]> intervalDeque = new ArrayDeque<>(); | ||
intervalDeque.add(intervals[0]); | ||
for(int i = 1; i < intervals.length; i++) { | ||
int[] lastElement = intervalDeque.getLast(); | ||
int[] nextElement = intervals[i]; | ||
|
||
if (lastElement[1] >= nextElement[0]) { | ||
int[] mergedElement = new int[]{ | ||
lastElement[0], | ||
Math.max(lastElement[1], nextElement[1]) | ||
}; | ||
intervalDeque.removeLast(); | ||
intervalDeque.add(mergedElement); | ||
} else { | ||
intervalDeque.add(nextElement); | ||
} | ||
} | ||
|
||
return intervalDeque.toArray(int[][]::new); | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 `O(n*logn)` 공간 복잡도는 `O(n)` 이다. (결과를 반환하기 위해 생성된 `int[][]`는 고려하지 않는다.) | ||
|
||
## 성능을 개선한 답안 (pointer 사용) | ||
|
||
문제를 잘 읽어보면 intervals 의 경우 start를 기준으로 이미 정렬이 되어있다고 하였기 떄문에 따로 정렬을 해줄 필요는 없다. | ||
for loop 에서는 start, end pointer를 이용해서 어느 구간이 병합되는지 기억해두고, 최종적으로 병합을 진행한다. | ||
|
||
start 가 -1 인 경우는 맨 오른쪽에 추가가 되어야 한다는 의미이고 | ||
end 가 -1 인 경우는 맨 왼쪽에 추가가되어야 한다는 의미이다. | ||
그 외에는 병합이 발생한 것이므로 병합처리를 진행한다. | ||
|
||
```java | ||
class Solution { | ||
public int[][] insert(int[][] intervals, int[] newInterval) { | ||
int start = -1; | ||
int end = -1; | ||
|
||
for (int i = 0; i < intervals.length; i++) { | ||
if (start == -1 && intervals[i][1] >= newInterval[0]) { | ||
start = i; | ||
} | ||
|
||
if (newInterval[1] >= intervals[i][0]) { | ||
end = i; | ||
} | ||
} | ||
|
||
if (start == -1) { | ||
int[][] newIntervals = Arrays.copyOf(intervals, intervals.length + 1); | ||
newIntervals[intervals.length] = newInterval; | ||
return newIntervals; | ||
} | ||
|
||
if (end == -1) { | ||
int[][] newIntervals = new int[intervals.length + 1][2]; | ||
newIntervals[0] = newInterval; | ||
System.arraycopy(intervals, 0, newIntervals, 1, newIntervals.length - 1); | ||
return newIntervals; | ||
} | ||
|
||
int[][] newIntervals = new int[intervals.length - (end - start)][2]; | ||
|
||
if (start >= 0) { | ||
System.arraycopy(intervals, 0, newIntervals, 0, start); | ||
} | ||
|
||
newIntervals[start] = new int[]{Math.min(intervals[start][0], newInterval[0]), Math.max(intervals[end][1], newInterval[1])}; | ||
|
||
if (intervals.length - (end + 1) >= 0) { | ||
System.arraycopy(intervals, end + 1, newIntervals, end + 1 - (end - start), intervals.length - (end + 1)); | ||
} | ||
|
||
return newIntervals; | ||
} | ||
} | ||
``` | ||
|
||
#### TC, SC | ||
|
||
시간 복잡도는 `O(n)` 공간 복잡도는 `O(1)` 이다. (결과를 반환하기 위해 생성된 `int[][]`는 고려하지 않는다.) |
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,61 @@ | ||
- 문제 | ||
- 유료: https://leetcode.com/problems/meeting-rooms-ii/ | ||
- 무료: https://www.lintcode.com/problem/919/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/07/22/leetcode-253 | ||
|
||
```java | ||
public class Solution { | ||
public int minMeetingRooms(List<Interval> intervals) { | ||
intervals = intervals.stream().sorted(Comparator.comparingInt(o -> o.start)).toList(); | ||
|
||
List<List<Interval>> days = new ArrayList<>(); | ||
|
||
for(Interval interval : intervals) { | ||
boolean added = false; | ||
for (List<Interval> day : days) { | ||
day.add(interval); | ||
if (canAttendMeetings(day)) { | ||
added = true; | ||
break; | ||
} | ||
day.remove(day.size() - 1); | ||
} | ||
|
||
if(!added) { | ||
List<Interval> newDay = new ArrayList<>(); | ||
newDay.add(interval); | ||
days.add(newDay); | ||
} | ||
} | ||
|
||
return days.size(); | ||
} | ||
|
||
public boolean canAttendMeetings(List<Interval> intervals) { | ||
for (int i = 0; i < intervals.size() - 1; i++) { | ||
if(intervals.get(i).end > intervals.get(i+1).start) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
class Interval { | ||
public int start, end; | ||
|
||
public Interval(int start, int end) { | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "{" + start + ", " + end + "}"; | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
days의 길이를 m 이라고 했을 때, 시간 복잡도는 `O(n^2 * m)` 공간 복잡도는 `O(n)` 이다. m은 최악의 경우 n이 될 수 있다. | ||
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,34 @@ | ||
- 문제: https://leetcode.com/problems/merge-intervals/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/07/23/leetcode-56 | ||
|
||
```java | ||
class Solution { | ||
public int[][] merge(int[][] intervals) { | ||
Arrays.sort(intervals, Comparator.comparingInt(o -> o[0])); | ||
|
||
Deque<int[]> intervalDeque = new ArrayDeque<>(); | ||
intervalDeque.add(intervals[0]); | ||
for(int i = 1; i < intervals.length; i++) { | ||
int[] lastElement = intervalDeque.getLast(); | ||
int[] nextElement = intervals[i]; | ||
|
||
if (lastElement[1] >= nextElement[0]) { | ||
int[] mergedElement = new int[]{ | ||
lastElement[0], | ||
Math.max(lastElement[1], nextElement[1]) | ||
}; | ||
intervalDeque.removeLast(); | ||
intervalDeque.add(mergedElement); | ||
} else { | ||
intervalDeque.add(nextElement); | ||
} | ||
} | ||
|
||
return intervalDeque.toArray(int[][]::new); | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 `O(n*logn)` 공간 복잡도는 `O(n)` 이다. (결과를 반환하기 위해 생성된 `int[][]`는 고려하지 않는다.) |
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,30 @@ | ||
- 문제: https://leetcode.com/problems/non-overlapping-intervals/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/07/23/leetcode-435 | ||
|
||
```java | ||
public int eraseOverlapIntervals(int[][] intervals) { | ||
int overlappingCount = 0; | ||
Arrays.sort(intervals, Comparator.comparingInt(o -> o[1])); | ||
|
||
int currentEnd = intervals[0][1]; | ||
for (int i = 0; i < intervals.length - 1; i++) { | ||
// overlapping 이 발생된 경우 | ||
if (currentEnd > intervals[i + 1][0]) { | ||
overlappingCount++; | ||
|
||
// 앞 interval 의 end 값이 뒤 interval 의 end 보다 작을 경우 이전 pointer 유지 | ||
if (currentEnd < intervals[i + 1][1]) { | ||
continue; | ||
} | ||
} | ||
|
||
currentEnd = intervals[i + 1][1]; | ||
} | ||
|
||
return overlappingCount; | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 `O(n*logn)` 공간 복잡도는 `O(1)` 이다. |
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,25 @@ | ||
- 문제: https://leetcode.com/problems/rotate-image/ | ||
- 풀이: https://algorithm.jonghoonpark.com/2024/07/22/leetcode-48 | ||
|
||
```java | ||
class Solution { | ||
public void rotate(int[][] matrix) { | ||
int l = matrix.length; | ||
|
||
int limit = (int) Math.ceil((double) l / 2); | ||
for (int i = 0; i < limit; i++) { | ||
for (int j = i; j < l - 1 - i; j++) { | ||
int temp = matrix[i][j]; | ||
matrix[i][j] = matrix[l - j - 1][i]; | ||
matrix[l - j - 1][i] = matrix[l - i - 1][l - j - 1]; | ||
matrix[l - i - 1][l - j - 1] = matrix[j][l - i - 1]; | ||
matrix[j][l - i - 1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 `O(n^2)` 공간 복잡도는 `O(1)` 이다. |
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.
일반적으로 복잡도 분석할 때 최악의 경우와 "일반적인"? 경우를 나누어서 분석하는게 좋을까요?
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.
미팅에서 나온 답변 :
일반적인 최악의 경우를 적을 것 (여기서는
n^3
), 실제 인터뷰에서 불필요하게 케이스를 나누면 공격받을 수 있음.