Skip to content

300. Longest Increasing Subsequence#32

Open
fhiyo wants to merge 1 commit intomainfrom
300_longest-increasing-subsequence
Open

300. Longest Increasing Subsequence#32
fhiyo wants to merge 1 commit intomainfrom
300_longest-increasing-subsequence

Conversation

@fhiyo
Copy link
Copy Markdown
Owner

@fhiyo fhiyo commented Jul 2, 2024


### ①

解法を覚えていた。lisが何の略かコメントで書くくらいしても良いと思った。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはほしいかなあと思います(phase3ぐらいでいいと思います)

if not nums:
return 0
lis_lengths = [1] * len(nums)
for i in reversed(range(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.

ちょっと細かい感じになっちゃいますが

for i in range(1, len(nums)):
    for j in range(i):
        if nums[j] >= nums[i]:
            continue
        lis_lengths[i] = max(lis_lengths[i], lis_lengths[j] + 1)

というふうにも変形できますね。こうするとreversedの文の新しくメモリ領域を作る操作と要素を逆転させる処理の文が節約できます。(全体がO(n^2)なので少しのチューニングにしかならないかもですが..)

lis = [] # longest increasing subsequence
for num in nums:
i = bisect_left(lis, num)
if i == len(lis):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は、

if i < len(lis):
    lis[i] = num
    continue
lis.append(num)

にします。趣味の範囲です。

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