Skip to content

703. Kth Largest Element in a Stream#9

Open
hemispherium wants to merge 1 commit intomainfrom
0703-kth-largest-element-in-a-stream
Open

703. Kth Largest Element in a Stream#9
hemispherium wants to merge 1 commit intomainfrom
0703-kth-largest-element-in-a-stream

Conversation

@hemispherium
Copy link
Owner

@hemispherium hemispherium self-assigned this Dec 15, 2025
class KthLargest {
public:
priority_queue<int, vector<int>, greater<int>> kth_largest;
int K;
Copy link

Choose a reason for hiding this comment

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

メンバ名のKが気になりました。大文字なので定数に見えたという感覚です。特に理由がなければ、Google C++ Style Guideなどを参考にk_にするかなと思いました。
https://google.github.io/styleguide/cppguide.html#Variable_Names

Suggested change
int K;
int k_;

Comment on lines +8 to +11
kth_largest.push(num);
if (kth_largest.size() > K) {
kth_largest.pop();
}
Copy link

Choose a reason for hiding this comment

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

add とほぼ同じなのでループで呼んでもいいですね。

@@ -0,0 +1,11 @@
### step1

値の小さい順のpriority queueにk個の要素を入れておいて、top()でk番目に大きい要素を返すというやり方を思いついて実装したが、細かい部分がちゃんと実装できていなかったのでGPTにスッキリ書き直してもらった。
Copy link

Choose a reason for hiding this comment

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

私はむしろこちらのほうがいいくらいの気持ちですね。無駄な push pop がないので。

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.

3 participants