Skip to content

Commit

Permalink
经典问题,最大子序和
Browse files Browse the repository at this point in the history
  • Loading branch information
gdis5251 committed Jun 4, 2021
1 parent caa70cb commit e91645c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 0 additions & 1 deletion .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions leet_code/53.最大子序和/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

func maxSubArray(nums []int) int {
dp := make([]int, len(nums))

// bce 优化
_ = nums[len(nums) - 1]
for index, num := range nums {
dp[index] = num
}

res := dp[0]

_ = dp[len(dp) - 1]
for i := 1; i < len(dp); i++ {
dp[i] += max(dp[i - 1], 0)

if res < dp[i] {
res = dp[i]
}
}

return res
}

func max(rhs, lhs int) int {
if rhs > lhs {
return rhs
}
return lhs
}

0 comments on commit e91645c

Please sign in to comment.