Skip to content

Commit

Permalink
【300.最长上升子序列】【Python】 (labuladong#507)
Browse files Browse the repository at this point in the history
* add python

* add python

* add python

Co-authored-by: LockyGuo <[email protected]>
Co-authored-by: labuladong <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2020
1 parent 424f210 commit 7b6cab8
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 动态规划系列/动态规划设计:最长递增子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,51 @@ public int lengthOfLIS(int[] nums) {

======其他语言代码======

```python 动态规划
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
f = [1] * (n)

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

res = 0
for i in range(n):
res = max(res, f[i])
return res
```

```python 二分查找
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
stack = []

def find_index(num):
l, r = 0, len(stack)
while l < r:
mid = l + r >> 1
if stack[mid] >= num:
r = mid
else:
l = mid + 1

return r


for num in nums:
if not stack or num > stack[-1]:
stack.append(num)
else:
position = find_index(num)
stack[position] = num

return len(stack)
```


[Kian](https://github.com/KianKw/) 提供 C++ 代码

```c++
Expand Down Expand Up @@ -257,3 +302,4 @@ public:
}
};
```

0 comments on commit 7b6cab8

Please sign in to comment.