-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path300LongestIncreasingSubsequence.py
More file actions
56 lines (50 loc) · 1.53 KB
/
300LongestIncreasingSubsequence.py
File metadata and controls
56 lines (50 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 16 00:29:30 2019
@author: alaric
"""
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
res = 0
dp = [1 for _ in range(len(nums))]
for i in range(len(nums)):
temp = 0
for j in range(i):
if nums[j] < nums[i]:
temp = max(temp , dp[j])
dp[i] = temp+1
res = max(res,dp[i])
return res
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
if n == 0 : return 0
dp = [1 for _ in range(n+1)]
for i in range(n):
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
# binary search
def lengthOfLIS(self, nums: List[int]) -> int:
def binarysearch( s , num):
l , r = 0 , len(s)-1
while l <= r:
m = int((l+r)/2)
if s[m] == num:
return m
elif s[m] > num:
r = m-1
else:
l = m+1
return l
if len(nums) == 0 :
return 0
solution = [nums[0]]
for i in range(len(nums)):
if nums[i] > solution[-1]:
solution.append(nums[i])
else:
index = binarysearch(solution , nums[i])
solution[index] = nums[i]
return len(solution)