Conversation
oda
reviewed
Apr 19, 2025
|
|
||
| class Solution { | ||
| public boolean canAttendMeetings(int[][] intervals) { | ||
| int[][] sortedIntervals = Arrays.copyOf(intervals, intervals.length); |
Owner
Author
There was a problem hiding this comment.
はい、.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.
Java 詳しくないのですが、この場合は比較するラムダ式は与える必要があるんでしたっけ。
Owner
Author
There was a problem hiding this comment.
はい、2次元配列の場合はJavaはint[]同士の並べ替え方法を知らないので、ラムダ式でどの要素を比較するのかを指定してあげる必要があります。
| - 会議の経過時間を start から end まで increment しながら刻んでいき Set に保存 | ||
| - 同時に Set から取り出し被りがあった時点で false を返す | ||
| - 感想 | ||
| - 実用的ではないが、こういう方法もあると参考のかと参考になった |
Owner
Author
There was a problem hiding this comment.
ありがとうございます。座標圧縮という言葉を初めて知りました。こちらも実装してみます。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題
https://leetcode.com/problems/meeting-rooms/
言語
Java
次に解く問題