Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions arai60/longest_increasing_subsequence/phase1.py
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):
Copy link

Choose a reason for hiding this comment

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

for j in range(i+1, len(nums)):
if nums[i] < nums[j] and lis_so_far[j] < lis_so_far[i] + 1:
Copy link

Choose a reason for hiding this comment

The 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)
29 changes: 29 additions & 0 deletions arai60/longest_increasing_subsequence/phase2.py
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])
Copy link

Choose a reason for hiding this comment

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

step2 のように、最後に 1 回 max() 関数を使ったほうがシンプルで読みやすい思います。

return max_lis

class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
MAX_NUM = 10 ** 4
increasing_subsequence = [MAX_NUM + 1] * len(nums)
Copy link

Choose a reason for hiding this comment

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

個人的には長さ 0 から始めて、どこにも挿入できない場合は末尾に追加する、といったロジックのほうが好みです。ですが、好みの問題だと思います。

Copy link

Choose a reason for hiding this comment

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

私は、10 ** 4 が(問題文の制約からくる)マジックナンバーであることが気になりますね。1でも大きくなったら動かなくなるわけですよねえ。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね, 訂正いたします。ところで, 逆にマジックナンバーをあえて使う場面などはあるのでしょうか...?

Copy link

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 num in nums:
index = bisect_left(increasing_subsequence, num)
increasing_subsequence[index] = num
return bisect_left(increasing_subsequence, MAX_NUM + 1)
10 changes: 10 additions & 0 deletions arai60/longest_increasing_subsequence/phase3.py
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
9 changes: 9 additions & 0 deletions arai60/longest_increasing_subsequence/phase4.py
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)
10 changes: 10 additions & 0 deletions arai60/longest_increasing_subsequence/phase5.py
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)