-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| 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): | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. step2 の書き方のほうがシンプルだと思います。 |
||
| lis_so_far[j] = lis_so_far[i] + 1 | ||
| return max(lis_so_far) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| """ | ||
| Reference | ||
| hayashi-ayさん: https://github.com/hayashi-ay/leetcode/pull/27/files | ||
| rossy0213さん: https://github.com/rossy0213/leetcode/pull/15/files | ||
| sakupan102さん: https://github.com/sakupan102/arai60-practice/pull/32/files 二分探索のところの議論がためになりました | ||
| Exzrgsさん: https://github.com/Exzrgs/LeetCode/pull/18/files | ||
|
|
||
| phase1の方法の動的計画法ではなくすることで最後のmax(lis_so_far)の作業をなくすことにした。 | ||
| """ | ||
|
|
||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| lis_so_far = [1] * len(nums) # lis: longest increasing subsequence | ||
| max_lis = 1 | ||
| for i in range(len(nums)): | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. step2 のように、最後に 1 回 |
||
| return max_lis | ||
|
|
||
| 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 私は、10 ** 4 が(問題文の制約からくる)マジックナンバーであることが気になりますね。1でも大きくなったら動かなくなるわけですよねえ。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. いや、読み手が分かるように書くので、マジックナンバーになったらコメントを書いてください。 有名なマジックナンバーとしては 0x5f3759df があります。 |
||
| for num in nums: | ||
| index = bisect_left(increasing_subsequence, num) | ||
| increasing_subsequence[index] = num | ||
| return bisect_left(increasing_subsequence, MAX_NUM + 1) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| lis_so_far = [1] * len(nums) # lis means longest increasing subsequence | ||
| max_lis = 1 | ||
| for i in range(1, len(nums)): | ||
| 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(lis_so_far[i], max_lis) | ||
| return max_lis |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| lis_so_far = [1] * len(nums) # lis means longest increasing subsequence | ||
| max_lis = 1 | ||
| for i in range(1, len(nums)): | ||
| for j in range(i): | ||
| if nums[j] < nums[i]: | ||
| lis_so_far[i] = max(lis_so_far[i], lis_so_far[j] + 1) | ||
| return max(lis_so_far) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| lis_so_far = [] | ||
| for num in nums: | ||
| index = bisect.bisect_left(lis_so_far, num) | ||
| if index == len(lis_so_far): | ||
| lis_so_far.append(num) | ||
| elif index < len(lis_so_far): | ||
| lis_so_far[index] = num | ||
| return len(lis_so_far) |
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.
-の左右に空白を空けることをお勧めいたします。https://peps.python.org/pep-0008/#other-recommendations
https://google.github.io/styleguide/pyguide.html#36-whitespace