# 300_最长上升子序列


"""

"""

# 方法一 动态规划
# 时间复杂度要求达到 o(nlogn)?? 意味着什么？。。意味着可能需采用二分查找
def lengthOfLIS1(nums):
n = len(nums)
dp = [1] * n

for i in range(1, n):
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j]+1)

return max(dp or [0])

# 方法二 二分法
def lengthOfLIS2(nums):
tails, res = [0]*len(nums), 0
for num in nums:
i, j = 0, res
while i < j:
m = (i+j) //2
if tails[m] < m:
i = m+1
else:
j = m
tails[i] = num
if j == res:
res += 1
return res

def lengthOfLIS3(nums):
#时间复杂度为O(n * log(n))
# if not nums: return 0
tails = [float('-inf')] * len(nums)
res = 0
for num in nums:
if num > tails[res - 1]:
tails[res] = num
res += 1
continue
left, right = 0, res - 1
while left < right:
mid = (left + right) // 2
if num <= tails[mid]:
right = mid
else:
left = mid + 1
tails[right] = num
return res

class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
# 堆数 piles = len(top)
top = []  # 堆顶元素数组

for poker in nums:  # poker 当前牌
# find left bound
# 其实质是right往left方向靠
left = 0
right = len(top)
while left < right:
mid = (left + right) // 2
if top[mid] == poker:
right = mid
elif top[mid] < poker:
left = mid + 1
elif top[mid] > poker:
right = mid

# "left" is the left bound
if left == len(top):
top.append(poker)
else:
top[left] = poker

return len(top)

nums = [10, 9, 2, 5, 3, 7, 101, 18]
print(lengthOfLIS1(nums))


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Lishu_cc

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
06-06

05-14
04-16
07-16
07-22 205
11-28
08-22 1069