Skip to content

703. Kth Largest Element in a Stream#10

Open
liruly wants to merge 2 commits intomainfrom
Arai60/703
Open

703. Kth Largest Element in a Stream#10
liruly wants to merge 2 commits intomainfrom
Arai60/703

Conversation

@liruly
Copy link
Owner

@liruly liruly commented Jan 17, 2026

703. Kth Largest Element in a Stream

- 時間計算量: O(N^2 logN)
- N回sortで毎回O(N logN)のsort
- nums.lengthが最大で10^4なのでTLE
- 大体O(10^9)を超えるとTLEする印象。競プロの話です
Copy link

Choose a reason for hiding this comment

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

O(109) という表記に違和感を感じます。ビッグオー表記は、計算量がパラメーター (N 等) に対して、どのような関数の形で表現されるかを表します。そのため、 O(???) の中に入る文言は、パラメーターに関する関数 (または 1) であるべきだと思います。
ビッグオー表記の中の関数にパラメーターの上限を代入すると、おおよその計算ステップ数が分かります。今回の場合は、「計算ステップ数が 109 を超えると TLE する印象」と書いたほうが伝わりやすいと思います。

また、計算ステップ数が 109 を超えると TLE する理由については、以下のコメントをご参照ください。
Yuto729/LeetCode_arai60#16 (comment)

Copy link
Owner Author

@liruly liruly Jan 18, 2026

Choose a reason for hiding this comment

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

計算ステップ数という表現があるのは存じ上げませんでした。ご指摘ありがとうございます。
また、多言語他言語の実行時間目安は知らなかったので勉強になりました。

Comment on lines +114 to +115
if (top_k_.size() > k_) {
top_k_.pop();
Copy link

Choose a reason for hiding this comment

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

この問題では必要ないですが、while文を使ってwhile (top_k_.size() > k_) のようにも書けますね。
こちらを好んでいる方もいらっしゃいました。参考

Copy link
Owner Author

@liruly liruly Jan 18, 2026

Choose a reason for hiding this comment

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

ありがとうございます。
確かにカプセル化に失敗したりで外部からデータを操作され想定以上のデータが入っていた場合の防御策にはなりますね。

ただそれをするなら、想定より値が少なかったときの処理も考えないとバランスが悪いかなと思いました。

この方式だとk+1番目以降のデータはもはや保持していないので下のコードのようにエラーを投げるしかないですが...

Copy link

Choose a reason for hiding this comment

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

確かに、外部からの注入なども考えると、要素が少ない時も考慮した方が堅牢ですね。
先程のコメントについては、コードが複雑になった時にミスなく読み書きしやすい、くらいの気持ちでした。
mamo3gr/arai60#25 (comment)

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