-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/300. Longest Increasing Subsequence #31
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: main
Are you sure you want to change the base?
Conversation
begin = middle + 1 | ||
return begin | ||
|
||
subseq_lens_to_nums: list[int] = [] |
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.
この変数名の意図を汲み取るのが難しかったです。
subseq_lens_to_nums: list[int] = [] | ||
for num in nums: | ||
insert_pos = get_subseq_len_to_num(subseq_lens_to_nums, num) | ||
if insert_pos < len(subseq_lens_to_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.
収まっている or 収まっていないで、それぞれ1つ処理があるだけの単純な構造なので、if-elseでも理解しやすいかと思いました。
end = len(sorted_list) | ||
while begin != end: | ||
middle = (begin + end) // 2 | ||
if num <= sorted_list[middle]: |
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.
middleが主役の分岐だと思うので、if sorted_list[middle] >= num
の方が自然なように感じました。
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.
主役 (比較される側) を左辺に持ってくる派と、数直線上に一直線に並ぶよう < または <= で原則統一する派があるように思います。
max_length = 0 | ||
for i in range(len(nums)): | ||
longest_subsequence_to_index[i] = 1 | ||
for j in range(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.
iとjの関係性がわかる変数名だとよりわかりやすいかと思いました。
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.
i -> end_index
j -> check_index
とかですかね
```python | ||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
subsequence_lengths_to_nums: list[int] = [] |
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.
to_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.
部分配列の長さから、その部分配列の最後の num のうち最小のものへのマッピングとみなせるため、 subsequence_lengths_to_nums で良いと思います。自分なら、 length_to_min_last_num と付けると思います。
def lengthOfLIS(self, nums: List[int]) -> int: | ||
subsequence_lengths_to_nums: list[int] = [] | ||
for num in nums: | ||
if not subsequence_lengths_to_nums or subsequence_lengths_to_nums[-1] < num: |
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.
ここ少し、if分岐がパズルに感じたため、
subsequence_lengths_to_nums
の初期値をnums[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.
subsequence_length = bisect_left(subsequence_lengths_to_nums, num)
if subsequence_length == len(subsequence_lengths_to_nums):
subsequence_lengths_to_nums.append(num)
else:
subsequence_lengths_to_nums[subsequence_length] = num
としたほうがシンプルだと思いました。
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.
確かにそれで行けますね、思いつきませんでした!
```python | ||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
longest_subsequence_to_index: dict[int, int] = {} |
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.
キーは配列のインデックスで、値はその要素を最後に使って作れる最大の長さだと思います。 longest_subsequence_to_index という変数名ですと、中に含まれている値と矛盾するように思います。 index_to_max_length はいかがでしょうか?
```python | ||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
subsequence_lengths_to_nums: list[int] = [] |
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.
部分配列の長さから、その部分配列の最後の num のうち最小のものへのマッピングとみなせるため、 subsequence_lengths_to_nums で良いと思います。自分なら、 length_to_min_last_num と付けると思います。
def lengthOfLIS(self, nums: List[int]) -> int: | ||
subsequence_lengths_to_nums: list[int] = [] | ||
for num in nums: | ||
if not subsequence_lengths_to_nums or subsequence_lengths_to_nums[-1] < num: |
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.
subsequence_length = bisect_left(subsequence_lengths_to_nums, num)
if subsequence_length == len(subsequence_lengths_to_nums):
subsequence_lengths_to_nums.append(num)
else:
subsequence_lengths_to_nums[subsequence_length] = num
としたほうがシンプルだと思いました。
end = len(sorted_list) | ||
while begin != end: | ||
middle = (begin + end) // 2 | ||
if num <= sorted_list[middle]: |
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://leetcode.com/problems/longest-increasing-subsequence/