Skip to content

Commit

Permalink
add post '(Leetcode) 253 - Meeting Room ii 풀이 (Meeting Schedule ii)'
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-jonghoonpark committed Jul 22, 2024
1 parent c8e8948 commit 1873814
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion _posts/2024-05-14-leetcode-252.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: (Leetcode) 252 - Meeting Schedule
title: (Leetcode) 252 - Meeting Room 풀이 (Meeting Schedule)
categories: [스터디-알고리즘]
tags:
[자바, java, 리트코드, Leetcode, 알고리즘, algorithm, list, 리스트, neetcode]
Expand Down
83 changes: 83 additions & 0 deletions _posts/2024-07-22-leetcode-253.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
layout: post
title: (Leetcode) 253 - Meeting Room ii 풀이 (Meeting Schedule ii)
categories: [스터디-알고리즘]
tags: [자바, java, 리트코드, Leetcode, 알고리즘, interval, array, sorting]
date: 2024-07-22 19:30:00 +0900
toc: true
---

기회가 되어 [달레님의 스터디](https://github.com/DaleStudy/leetcode-study)에 참여하여 시간이 될 때마다 한문제씩 풀어보고 있다.

[https://neetcode.io/practice](https://neetcode.io/practice)

---

- 문제
- 유료: [https://leetcode.com/problems/meeting-rooms-ii/](https://leetcode.com/problems/meeting-rooms-ii/)
- 무료: [https://neetcode.io/problems/meeting-schedule-ii](https://neetcode.io/problems/meeting-schedule-ii)

## 내가 작성한 풀이

지난번에 풀었던 [meeting room](https://algorithm.jonghoonpark.com/2024/05/14/leetcode-252) 문제의 심화버전이다.
해당 문제에서 사용했던 풀이를 재활용하였다.

먼저 intervals를 정렬한다.
이후 반복문을 통해 interval을 추가해나가는데, 만약 겹치는 시간대가 있을 경우 새로운 날짜를 생성한다.

```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 이 될 수 있다.

0 comments on commit 1873814

Please sign in to comment.