-
Notifications
You must be signed in to change notification settings - Fork 0
/
300.py
38 lines (32 loc) · 1002 Bytes
/
300.py
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
from typing import List
# Name: Longest Increasing Subsequence
# Link: https://leetcode.com/problems/longest-increasing-subsequence/
# Method: Compressed DP, previous largest subsequence
# Time: O(n\*log(n))
# Space: O(n)
# Difficulty: Medium
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
if not nums:
return 0
seq = [nums[0]]
for i in range(1, len(nums)):
insert_poz = self.bisect(seq, nums[i])
if insert_poz == len(seq):
seq.append(nums[i])
else:
seq[insert_poz] = nums[i]
return len(seq)
@staticmethod
def bisect(arr: List[int], target: int) -> int:
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if target > arr[mid]:
low = mid + 1
elif target < arr[mid]:
high = mid - 1
else:
return mid
return low