Skip to content

300.longest increasing subsequence#29

Open
tom4649 wants to merge 4 commits intomainfrom
300.Longest-Increasing-Subsequence
Open

300.longest increasing subsequence#29
tom4649 wants to merge 4 commits intomainfrom
300.Longest-Increasing-Subsequence

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 27, 2026

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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの問題かとは思いますが,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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

length_of_isよりもlength_of_increasing_sequenceと省略しない流儀の方が現代では一般的なようです(cf).
lenの方は省略しても(比較的よくみるので)良いかもしれません:len_increasing_subsequence

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が疲れているから良いかと思ったのですが、isだと伝わらないかもしれませんね。
省略は極力避けた方が良さそうです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には len を省略するなら、len_is まで省略しても許容です。それならそれで is が何なのかコメントで補足してもらえると有り難いです。

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.

長いので、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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

maxだと線形時間かかるので,length_of_is[-1]でも良さそうですね.
もっともループが多項式(2乗)時間なので線形時間ならボトルネックにはならず,大した改善にはならなさそうですが.

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.

いえ、ここはlength_of_is[-1]ではなくmaxを取る必要があります

Copy link
Copy Markdown

@5ky7 5ky7 Mar 28, 2026

Choose a reason for hiding this comment

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

あくまで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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

lisという文字からは,整数型ではなく配列(リスト)を想像しました.
len_lis_with_tail_nとするのはいかがでしょうか.

個人的にはpos_insert_nにして+ 1を外すのが好みです.

Suggested change
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

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.

そのまま採用させていただきました

return bisect_left(coords, x) + 1

bit = BITMax(len(coords))
ans = 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.

ans は設問に対する回答というニュアンスを感じました。変数に格納される値の意味や役割に寄せた命名のほうが良いと思います。 max_length はいかがでしょうか?

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.

Geminiに伝えておきます
いや、読んで修正しなかった自分が気をつけようと思います

def num_to_rank(n):
return bisect_left(nums_sorted, n) + 1

seg_tree = SegTree(len(nums))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)

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.

segment_treeの方が良さそうですね。極力省略しない、なかなか身につかないので気をつけます


class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
tails = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

3か月くらい後にこのコードを読むと、tails が何を表していて、なぜ問題が解けるのか読み解くのに苦労しそうです。何らかコメントでサポートできると良いと思います。

Copy link
Copy Markdown
Owner Author

@tom4649 tom4649 Mar 28, 2026

Choose a reason for hiding this comment

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

# 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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rank という概念は本問側のもので、それがこっちに染み出しているように見えます。

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.

今回の問題に依存した変数名なのは改善すべきですね。
indexに変更しました。

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.

4 participants