Open
Conversation
fuga-98
reviewed
Apr 17, 2025
| ```python | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| if nums is None: |
There was a problem hiding this comment.
Type関数を使っても良いかもしれません。
https://docs.python.org/ja/3.13/library/functions.html#type
oda
reviewed
Apr 18, 2025
| 変数名も含めて読みやすいように変えてみる。 | ||
| 最初の`if not nums`はこのままだと、numsにNoneが渡されても0を返してしまう。型ヒントからnumsにNoneは来ないことがわかっているが、 | ||
| もし来た場合、例外を出して早めにバグを顕在化してほしいなと思ったので、TypeErrorを出すようにした。 | ||
| あと最近Design by Contractとか防御的プログラミングの概念を本(Good Code, Bad Code)で学習したので、このあたりはチームの方針にもよるのかなと思った。 |
There was a problem hiding this comment.
「ユーザーフェイシングかどうか」など何を作っているかの状況によるかと思います。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.q026dzgqj28b
| tails = [] | ||
| for num in nums: | ||
| insert_position = bisect_left(tails, num) | ||
| if insert_position == len(tails): |
There was a problem hiding this comment.
私は、insert_position < len(tails) を条件にして上下入れ替えると思いますが、趣味の範囲ですね。
| return len(tails) | ||
| ``` | ||
|
|
||
| ほかにも Fenwick Tree(Binary Indexed Tree)やセグメントツリーを使っても解けるらしいが、 |
There was a problem hiding this comment.
セグメントツリーはアイディアとしてどういう構造化を知っておくといいかもしれません(、私も、使わない問題でも発想の過程で使ったりします)が、知らなくても構わないくらいかと思います。パズルとして出題しやすい子なんですよ。
nodchip
reviewed
Apr 18, 2025
| left = 0 | ||
| right = len(tails) | ||
| while left < right: | ||
| mid = left + (right - left) // 2 |
There was a problem hiding this comment.
Python では int 型は多倍長整数で実装されています。そのため、オーバーフローを回避するような書き方はする必要がなく、 mid = (left + right) // 2 とシンプルに書いてしまって大丈夫だと思います。
hroc135
reviewed
Apr 19, 2025
| if nums is None: | ||
| raise TypeError("nums must be a List, not None") | ||
| if len(nums) == 0: | ||
| return 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/longest-increasing-subsequence/description/