Skip to content

252. Meeting Rooms#20

Open
katsukii wants to merge 5 commits intomainfrom
252
Open

252. Meeting Rooms#20
katsukii wants to merge 5 commits intomainfrom
252

Conversation

@katsukii
Copy link
Owner

問題

https://leetcode.com/problems/meeting-rooms/

Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Example 2:

Input: intervals = [[7,10],[2,4]]
Output: true
 

Constraints:

0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106

言語

Java

次に解く問題

  1. Meeting Rooms II


class Solution {
public boolean canAttendMeetings(int[][] intervals) {
int[][] sortedIntervals = Arrays.copyOf(intervals, intervals.length);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.clone がありますか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、.cloneでもいけますが、shallow copy になるため避けていました。

int[][] sortedIntervals = intervals.clone();

ただ、これを書く時に念の為調べたら上記のArrays.copyOfもshallow copyだったので同じ挙動になりますね。。deep copyするなら for文で intervals[i].clone(); をまわすか、Java8以降のStream APIを使って以下のように書くようです。

int[][] sortedIntervals = Arrays.stream(intervals)
    .map(int[]::clone)
    .toArray(int[][]::new);

```java
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java 詳しくないのですが、この場合は比較するラムダ式は与える必要があるんでしたっけ。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、2次元配列の場合はJavaはint[]同士の並べ替え方法を知らないので、ラムダ式でどの要素を比較するのかを指定してあげる必要があります。

- 会議の経過時間を start から end まで increment しながら刻んでいき Set に保存
- 同時に Set から取り出し被りがあった時点で false を返す
- 感想
- 実用的ではないが、こういう方法もあると参考のかと参考になった
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

座標圧縮と組み合わせると、increment の回数が減らせます。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。座標圧縮という言葉を初めて知りました。こちらも実装してみます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants