Skip to content

300. Longest Increasing Subsequence#29

Open
5103246 wants to merge 1 commit intomainfrom
300-longest-increasing-subsequence
Open

300. Longest Increasing Subsequence#29
5103246 wants to merge 1 commit intomainfrom
300-longest-increasing-subsequence

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented Oct 29, 2025

Comment on lines +27 to +30
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> subsequence{nums[0]};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

問題の制約上ありえないですが、先に空配列を弾いておいてもいいかもしれません。

- https://github.com/colorbox/leetcode/pull/45/files#diff-9b5a29ecc5f6f85f4ebca7124750d32df099c24d4b0f711e2df2513f6b931a28
- lower_boundの返り値が.end()だったら、pushしてそれ以外なら値を更新すれば、もっとシンプルになるな
- https://github.com/irohafternoon/LeetCode/pull/34/files
- 変数名はlis_elementsが良さげに感じた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この変数は、各長さごとのsubsequenceの尻尾の数字を記録し、同じ長さが複数見つかったら値が最小な尻尾を記録するという役割なので、自分ならmin_tailsとし、さらにコメントで、// min_tails[i] is minimum of tail of length i + 1 subsequences とさらに補足します。

他には、マッピングのニュアンスも含めて length_to_min_tail または length_to_min_last_num などのアイデアもあります。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名だけからはちょっと大変でコメントで補足したほうがいい気がしますねー。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
lis_elementsだけじゃ役割がよくわからなかったですね。
変数名だけじゃわかりづらかったら、コメントで補足するようにしていきます。

- https://github.com/colorbox/leetcode/pull/45/files#diff-9b5a29ecc5f6f85f4ebca7124750d32df099c24d4b0f711e2df2513f6b931a28
- lower_boundの返り値が.end()だったら、pushしてそれ以外なら値を更新すれば、もっとシンプルになるな
- https://github.com/irohafternoon/LeetCode/pull/34/files
- 変数名はlis_elementsが良さげに感じた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名だけからはちょっと大変でコメントで補足したほうがいい気がしますねー。

};
```

- イテレータを使う場合のlower_boundを実装した。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

二分探索の cmov (conditional move) を使った最適化というのがあるので、ちょっと見ておいてもいいかもしれません。
https://discord.com/channels/1084280443945353267/1251052599294296114/1265673894917050493

二分探索は分岐予測が当たらないので遅いという性質があるのでそれを避けるための工夫です。

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