pythonfrom typing import List
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
if not nums:
return 0
dp = []
for i in range(len(nums)):
dp.append(1)
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
return len(dp)
print(Solution().lengthOfLIS([3, 2, 1, 1, 2]))
pythonfrom typing import List
from bisect import bisect_left
class Solution:
def maxEnvelopes(self, arr: List[List[int]]) -> int:
# sort increasing in first dimension and decreasing on second
arr.sort(key=lambda x: (x[0], -x[1]))
def lis(nums):
dp = []
for i in range(len(nums)):
idx = bisect_left(dp, nums[i])
if idx == len(dp):
dp.append(nums[i])
else:
dp[idx] = nums[i]
return len(dp)
# extract the second dimension and run the LIS
return lis([i[1] for i in arr])
print(Solution().maxEnvelopes([[5,4],[6,4],[6,7],[2,3]]))
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!