Skip to content

Conversation

Satorien
Copy link
Owner

begin = middle + 1
return begin

subseq_lens_to_nums: list[int] = []

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):

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]:

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の方が自然なように感じました。

Copy link

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):

Choose a reason for hiding this comment

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

iとjの関係性がわかる変数名だとよりわかりやすいかと思いました。

Copy link
Owner Author

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] = []

Choose a reason for hiding this comment

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

to_numsは、何かマッピングを行なっているわけではなければ不要かと感じました。

Copy link

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:

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]を入れてしまって、特別扱いしても良いかな、と思いました。好みだと思います。

Copy link

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

としたほうがシンプルだと思いました。

Copy link
Owner Author

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] = {}
Copy link

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] = []
Copy link

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

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]:
Copy link

Choose a reason for hiding this comment

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

主役 (比較される側) を左辺に持ってくる派と、数直線上に一直線に並ぶよう < または <= で原則統一する派があるように思います。

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