class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
def dfs(i, j):
if i == len(nums):
return 0
LIS = dfs(i + 1, j) # not include
if j == -1 or nums[j] < nums[i]:
LIS = max(LIS, 1 + dfs(i + 1, i)) # include
return LIS
return dfs(0, -1)class Solution:
def lengthOfLIS(self, nums):
n = len(nums)
memo = [[-1] * (n + 1) for _ in range(n)]
def dfs(i, j):
if i == n:
return 0
if memo[i][j + 1] != -1:
return memo[i][j + 1]
LIS = dfs(i + 1, j)
if j == -1 or nums[j] < nums[i]:
LIS = max(LIS, 1 + dfs(i + 1, i))
memo[i][j + 1] = LIS
return LIS
return dfs(0, -1)class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
memo = [-1] * n
def dfs(i):
if memo[i] != -1:
return memo[i]
LIS = 1
for j in range(i + 1, n):
if nums[i] < nums[j]:
LIS = max(LIS, 1 + dfs(j))
memo[i] = LIS
return LIS
return max(dfs(i) for i in range(n))class Solution:
def lengthOfLIS(self, nums):
n = len(nums)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
for j in range(i - 1, -2, -1):
LIS = dp[i + 1][j + 1] # Not including nums[i]
if j == -1 or nums[j] < nums[i]:
LIS = max(LIS, 1 + dp[i + 1][i + 1]) # Including nums[i]
dp[i][j + 1] = LIS
return dp[0][0]class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
LIS = [1] * len(nums)
for i in range(len(nums) - 1, -1, -1):
for j in range(i + 1, len(nums)):
if nums[i] < nums[j]:
LIS[i] = max(LIS[i], 1 + LIS[j])
return max(LIS)from bisect import bisect_left
class SegmentTree:
def __init__(self, N):
self.n = N
while (self.n & (self.n - 1)) != 0:
self.n += 1
self.tree = [0] * (2 * self.n)
def update(self, i, val):
self.tree[self.n + i] = val
j = (self.n + i) >> 1
while j >= 1:
self.tree[j] = max(self.tree[j << 1], self.tree[j << 1 | 1])
j >>= 1
def query(self, l, r):
if l > r:
return 0
res = float('-inf')
l += self.n
r += self.n + 1
while l < r:
if l & 1:
res = max(res, self.tree[l])
l += 1
if r & 1:
r -= 1
res = max(res, self.tree[r])
l >>= 1
r >>= 1
return res
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
def compress(arr):
sortedArr = sorted(set(arr))
order = []
for num in arr:
order.append(bisect_left(sortedArr, num))
return order
nums = compress(nums)
n = len(nums)
segTree = SegmentTree(n)
LIS = 0
for num in nums:
curLIS = segTree.query(0, num - 1) + 1
segTree.update(num, curLIS)
LIS = max(LIS, curLIS)
return LISfrom bisect import bisect_left
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = []
dp.append(nums[0])
LIS = 1
for i in range(1, len(nums)):
if dp[-1] < nums[i]:
dp.append(nums[i])
LIS += 1
continue
idx = bisect_left(dp, nums[i])
dp[idx] = nums[i]
return LIS