Skip to content

Conversation

@potrue
Copy link
Owner

@potrue potrue commented Aug 24, 2025

}
int max_num_rooms = 0;
int num_rooms = 0;
for (const pair<int, int>& [_, num_change] : time_to_num_change) {

Choose a reason for hiding this comment

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

上で書かれているように auto を使っても良いかなと思いました。num_change が int だろうと下の行でわかりそうで、使わない _ の型を書くのは微妙かなという理由です。

Copy link
Owner Author

@potrue potrue Aug 24, 2025

Choose a reason for hiding this comment

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

これは確かにそうですね。読むにあたってノイズとなる情報かもしれません。

num_rooms += num_change;
max_num_rooms = max(max_num_rooms, num_rooms);
}
return max_num_rooms;

Choose a reason for hiding this comment

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

問題文の表現に合わせて min を使った方が自然かなと思いました。

https://github.com/ryosuketc/leetcode_arai60/pull/56/files
https://github.com/Ryotaro25/leetcode_first60/pull/61/files

cppのmapやunordered_mapはpythonでいうdefaultdictみたいな振る舞いをするらしい。([]で存在しない要素にアクセスしようとすると自動でvalueがコンストラクタから初期化される)

Choose a reason for hiding this comment

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

レファレンスはこの辺りでしょうか。
https://en.cppreference.com/w/cpp/container/map/operator_at.html

Returns a reference to the value that is mapped to a key equivalent to key or x respectively, performing an insertion if such key does not already exist.

++time_to_num_change[interval[0]];
--time_to_num_change[interval[1]];
}
int max_num_rooms = 0;

Choose a reason for hiding this comment

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

好みですが、min_required_rooms などの方が素直に読めそうだなとは思います (ただ関数の返り値であることは明らかなので、これで十分とは思いますが)。

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.

4 participants