Conversation
300/sol1.py
Outdated
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| length_of_is = [1] * len(nums) | ||
| for i, n in enumerate(nums): |
There was a problem hiding this comment.
好みの問題かとは思いますが,nよりnumの方が好みです.nは意味が広く,例えばlen(nums)にも使われうると感じるためコードが長くなると勘違いしうる一方で,numならnumsの要素だと特定できる気がするからです.
この程度のスコープならnで良いという意見ももっともだとは思います.
300/sol1.py
Outdated
| @@ -0,0 +1,9 @@ | |||
| class Solution: | |||
| def lengthOfLIS(self, nums: List[int]) -> int: | |||
| length_of_is = [1] * len(nums) | |||
There was a problem hiding this comment.
length_of_isよりもlength_of_increasing_sequenceと省略しない流儀の方が現代では一般的なようです(cf).
lenの方は省略しても(比較的よくみるので)良いかもしれません:len_increasing_subsequence
There was a problem hiding this comment.
関数名にLISが疲れているから良いかと思ったのですが、isだと伝わらないかもしれませんね。
省略は極力避けた方が良さそうです。
There was a problem hiding this comment.
個人的には len を省略するなら、len_is まで省略しても許容です。それならそれで is が何なのかコメントで補足してもらえると有り難いです。
There was a problem hiding this comment.
長いので、len_is を採用しました。コメントも追加しました
300/sol1.py
Outdated
| if n > nums[j]: | ||
| length_of_is[i] = max(length_of_is[i], length_of_is[j] + 1) | ||
|
|
||
| return max(length_of_is) |
There was a problem hiding this comment.
maxだと線形時間かかるので,length_of_is[-1]でも良さそうですね.
もっともループが多項式(2乗)時間なので線形時間ならボトルネックにはならず,大した改善にはならなさそうですが.
There was a problem hiding this comment.
いえ、ここはlength_of_is[-1]ではなくmaxを取る必要があります
There was a problem hiding this comment.
あくまでnが最後尾にくる部分列の最大長がlength_of_isに入っているので,おっしゃる通りmaxを取る必要がありますね.
失礼いたしました.
300/sol2.py
Outdated
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| tails = [] | ||
| for n in nums: | ||
| lis_if_tail_is_n = bisect_left(tails, n) + 1 |
There was a problem hiding this comment.
lisという文字からは,整数型ではなく配列(リスト)を想像しました.
len_lis_with_tail_nとするのはいかがでしょうか.
個人的にはpos_insert_nにして+ 1を外すのが好みです.
| lis_if_tail_is_n = bisect_left(tails, n) + 1 | |
| pos_insert_n = bisect_left(tails, n) | |
| if pos_insert_n == len(tails): | |
| tails.append(n) | |
| continue | |
| tails[pos_insert_n] = n |
| return bisect_left(coords, x) + 1 | ||
|
|
||
| bit = BITMax(len(coords)) | ||
| ans = 0 |
There was a problem hiding this comment.
ans は設問に対する回答というニュアンスを感じました。変数に格納される値の意味や役割に寄せた命名のほうが良いと思います。 max_length はいかがでしょうか?
There was a problem hiding this comment.
Geminiに伝えておきます
いや、読んで修正しなかった自分が気をつけようと思います
| def num_to_rank(n): | ||
| return bisect_left(nums_sorted, n) + 1 | ||
|
|
||
| seg_tree = SegTree(len(nums)) |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)
There was a problem hiding this comment.
segment_treeの方が良さそうですね。極力省略しない、なかなか身につかないので気をつけます
|
|
||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| tails = [] |
There was a problem hiding this comment.
3か月くらい後にこのコードを読むと、tails が何を表していて、なぜ問題が解けるのか読み解くのに苦労しそうです。何らかコメントでサポートできると良いと思います。
There was a problem hiding this comment.
# tails[i] is the smallest ending element of all increasing subsequences of length (i + 1)
とコメントを追加しました
300/sol3.py
Outdated
| def _to_leaf_index(self, index): | ||
| return index + self.offset - 1 | ||
|
|
||
| def update(self, rank, value): |
There was a problem hiding this comment.
rank という概念は本問側のもので、それがこっちに染み出しているように見えます。
There was a problem hiding this comment.
今回の問題に依存した変数名なのは改善すべきですね。
indexに変更しました。
https://leetcode.com/problems/longest-increasing-subsequence/submissions/1960476790/