-
Notifications
You must be signed in to change notification settings - Fork 0
252. Meeting Rooms #20
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
base: main
Are you sure you want to change the base?
Conversation
|
||
class Solution { | ||
public boolean canAttendMeetings(int[][] intervals) { | ||
int[][] sortedIntervals = Arrays.copyOf(intervals, intervals.length); |
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.
.clone がありますか?
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.
はい、.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])); |
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.
Java 詳しくないのですが、この場合は比較するラムダ式は与える必要があるんでしたっけ。
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.
はい、2次元配列の場合はJavaはint[]同士の並べ替え方法を知らないので、ラムダ式でどの要素を比較するのかを指定してあげる必要があります。
- 会議の経過時間を start から end まで increment しながら刻んでいき Set に保存 | ||
- 同時に Set から取り出し被りがあった時点で false を返す | ||
- 感想 | ||
- 実用的ではないが、こういう方法もあると参考のかと参考になった |
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.
座標圧縮と組み合わせると、increment の回数が減らせます。
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.
ありがとうございます。座標圧縮という言葉を初めて知りました。こちらも実装してみます。
問題
https://leetcode.com/problems/meeting-rooms/
言語
Java
次に解く問題