Skip to content

solve: 252.Meeting Rooms#55

Open
t9a-dev wants to merge 2 commits intomainfrom
252.Meeting-Rooms
Open

solve: 252.Meeting Rooms#55
t9a-dev wants to merge 2 commits intomainfrom
252.Meeting-Rooms

Conversation

@t9a-dev
Copy link
Owner

@t9a-dev t9a-dev commented Feb 28, 2026

問題: 252. Meeting Rooms(NeetCode版)
次に解く問題: 253. Meeting Rooms Ⅱ(NeetCode版)
ファイルの構成: ./src/bin/<各ステップ>.rs

intervals.sort_by_key(|v| v.start);
intervals.windows(2).all(|w| {
let (interval, next_interval) = (&w[0], &w[1]);
interval.end <= next_interval.start
Copy link

Choose a reason for hiding this comment

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

Rustは全然わからないんですが読めたので多分読みやすいコードなのだと思います(あんまりレビューできずすみません。。)
ただの感想ですがwindowsなんていうメソッドがRustにはあるんですね。機会があれば学んでみたいです。

関数のシグネチャがNeetCode採点システムに適合しません。
*/
pub fn can_attend_meetings(intervals: &[Interval]) -> bool {
let mut time_to_use_room_key_count = BinaryHeap::new();
Copy link

Choose a reason for hiding this comment

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

全部 push し終えたら後は pop するだけなので、ヒープを使わなくても配列をソートするだけで済みそうですね。それで結局 step3 のアルゴリズムに行きつきますね

Comment on lines +57 to +60
intervals.windows(2).all(|w| {
let (interval, next_interval) = (&w[0], &w[1]);
interval.end <= next_interval.start
})

Choose a reason for hiding this comment

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

Rustあまり知らないのですが、L58を書かなければいけないのが少し気になりました。
use itertools::Itertools; とすると、可読性は上がるかなと思いました。

Suggested change
intervals.windows(2).all(|w| {
let (interval, next_interval) = (&w[0], &w[1]);
interval.end <= next_interval.start
})
intervals.iter().tuple_windows().all(
|(interval, next_interval)| interval.end <= next_interval.start
)

impl Solution {
pub fn can_attend_meetings(mut intervals: Vec<Interval>) -> bool {
intervals.sort_by_key(|v| v.start);
intervals.windows(2).all(|w| {
Copy link

Choose a reason for hiding this comment

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

windows 結構強力ですね。
Python だと itertools.pairwise というのがあるんですが、あまり使われているのをみたことがありません。

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.

5 participants