mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
@ -175,31 +175,25 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
|
||||||
func maxSubArray(nums []int) int {
|
|
||||||
if len(nums)<1{
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
dp:=make([]int,len(nums))
|
|
||||||
result:=nums[0]
|
|
||||||
dp[0]=nums[0]
|
|
||||||
for i:=1;i<len(nums);i++{
|
|
||||||
dp[i]=max(dp[i-1]+nums[i],nums[i])
|
|
||||||
result=max(dp[i],result)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func max(a,b int)int{
|
```go
|
||||||
if a>b{
|
func maxSubArray(nums []int) int {
|
||||||
return a
|
maxSum := nums[0]
|
||||||
}else{
|
for i := 1; i < len(nums); i++ {
|
||||||
return b
|
if nums[i] + nums[i-1] > nums[i] {
|
||||||
|
nums[i] += nums[i-1]
|
||||||
|
}
|
||||||
|
if nums[i] > maxSum {
|
||||||
|
maxSum = nums[i]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return maxSum
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
@ -135,8 +135,33 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
func lengthOfLIS(nums []int ) int {
|
||||||
|
dp := []int{}
|
||||||
|
for _, num := range nums {
|
||||||
|
if len(dp) ==0 || dp[len(dp) - 1] < num {
|
||||||
|
dp = append(dp, num)
|
||||||
|
} else {
|
||||||
|
l, r := 0, len(dp) - 1
|
||||||
|
pos := r
|
||||||
|
for l <= r {
|
||||||
|
mid := (l + r) >> 1
|
||||||
|
if dp[mid] >= num {
|
||||||
|
pos = mid;
|
||||||
|
r = mid - 1
|
||||||
|
} else {
|
||||||
|
l = mid + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp[pos] = num
|
||||||
|
}//二分查找
|
||||||
|
}
|
||||||
|
return len(dp)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
*复杂度分析*
|
||||||
|
- 时间复杂度:O(nlogn)。数组 nums 的长度为 n,我们依次用数组中的元素去更新 dp 数组,相当于插入最后递增的元素,而更新 dp 数组时需要进行 O(logn) 的二分搜索,所以总时间复杂度为 O(nlogn)。
|
||||||
|
- 空间复杂度:O(n),需要额外使用长度为 n 的 dp 数组。
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user