-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0300-longest-increasing-subsequence.md #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
お疲れさまです。自分はまだ解いていないのと理解できていない問題なので、あほなコメントとして読んでもらえればと思います。
https://discord.com/channels/1084280443945353267/1196498607977799853/1238849107267948575
| min_tails = [] | ||
| # min_tails[i] is minimum of tail of length i + 1 subsequences | ||
| for num in nums: | ||
| next_length = bisect_left(min_tails, num) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここで+1してnext_lengthを調整したあとに、227行目で-1しているのが気になりました。
今の自分には問題が難し過ぎて理解できていないので、読み流してもらって大丈夫です。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
いえいえ、自分もその違和感は感じていて、どうしたものかと思いながら書いていました。
というのも、lengthは1-indexedな概念で、これを軸にまとめた方が条件分岐などが読みやすい気がするものの、bisectの結果やmin_tailsのアクセスは、length - 1の0-indexedだからです。
変数も0-indexedで扱い、"index"などの変数名にするか、それともmin_tailsなどの配列側を1-indexedで扱えるように、0番目に番兵を仕込んでおくのも手かなと思いますが、現状のコードがベストだと考えました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
というのも、lengthは1-indexedな概念で、これを軸にまとめた方が条件分岐などが読みやすい気がするものの、bisectの結果やmin_tailsのアクセスは、length - 1の0-indexedだからです。
なるほど。そういった性質を持つ変数であれば、+1,-1による調整を明示的にしておくことで読み手に調整が必要な値であることを示せるという点で良いなと思いました。(無理に隠蔽してしまうと読み手が変更などの際に注意を払えないのでバグになりやすくなるなど。)
ありがとうございました。
|
|
||
| ### 実装3 | ||
|
|
||
| - セグメント木を使ってみる。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
セグメント木はソフトウェアエンジニアの常識には含まれていないと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
セグメントツリーは、50行以内で書け、いろいろな複雑なデータ構造の代用品として使えるので重宝されます。
https://discord.com/channels/1084280443945353267/1288758969522978847/1300830631512506448
| # return [] | ||
| return 0 | ||
|
|
||
| min_tails = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分なら length_to_min_tail または length_to_min_last_num とすると思います。コメントが書かれているので、 min_tails でも十分伝わると思います。
この問題:https://leetcode.com/problems/longest-increasing-subsequence/
次の問題:https://leetcode.com/problems/maximum-subarray/