-
Notifications
You must be signed in to change notification settings - Fork 0
Longest increasing subsequence #28
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: arai60
Are you sure you want to change the base?
Conversation
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| lis_so_far = [1] * len(nums) # lis: longest increasing subsequence | ||
| for i in range(len(nums)-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.
| lis_so_far = [1] * len(nums) # lis: longest increasing subsequence | ||
| for i in range(len(nums)-1): | ||
| for j in range(i+1, len(nums)): | ||
| if nums[i] < nums[j] and lis_so_far[j] < lis_so_far[i] + 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.
step2 の書き方のほうがシンプルだと思います。
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| MAX_NUM = 10 ** 4 | ||
| increasing_subsequence = [MAX_NUM + 1] * len(nums) |
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.
個人的には長さ 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.
私は、10 ** 4 が(問題文の制約からくる)マジックナンバーであることが気になりますね。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.
確かにそうですね, 訂正いたします。ところで, 逆にマジックナンバーをあえて使う場面などはあるのでしょうか...?
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.
いや、読み手が分かるように書くので、マジックナンバーになったらコメントを書いてください。
有名なマジックナンバーとしては 0x5f3759df があります。
https://en.wikipedia.org/wiki/Fast_inverse_square_root
| for j in range(i): | ||
| if nums[j] < nums[i]: | ||
| lis_so_far[i] = max(lis_so_far[i], lis_so_far[j] + 1) | ||
| max_lis = max(max_lis, lis_so_far[i]) |
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.
step2 のように、最後に 1 回 max() 関数を使ったほうがシンプルで読みやすい思います。
問題
300. Longest Increasing Subsequence