Skip to content

Commit

Permalink
Update 0300.最长上升子序列.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghongcheng authored Jun 6, 2023
1 parent 2261538 commit d3a07fa
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion problems/0300.最长上升子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Solution {
}
}
```

DP
Python:
```python
class Solution:
Expand All @@ -157,7 +157,31 @@ class Solution:
result = max(result, dp[i]) #取长的子序列
return result
```
贪心
```python
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
if len(nums) <= 1:
return len(nums)

tails = [nums[0]] # 存储递增子序列的尾部元素
for num in nums[1:]:
if num > tails[-1]:
tails.append(num) # 如果当前元素大于递增子序列的最后一个元素,直接加入到子序列末尾
else:
# 使用二分查找找到当前元素在递增子序列中的位置,并替换对应位置的元素
left, right = 0, len(tails) - 1
while left < right:
mid = (left + right) // 2
if tails[mid] < num:
left = mid + 1
else:
right = mid
tails[left] = num

return len(tails) # 返回递增子序列的长度

```
Go:
```go
// 动态规划求解
Expand Down

0 comments on commit d3a07fa

Please sign in to comment.