mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 02:53:42 +00:00
Update 42. 连续子数组的最大和.md
补充连续子数组最大和的解题思路说明,并补充具体数值的状态推导示例。
This commit is contained in:
@@ -8,6 +8,14 @@
|
||||
|
||||
## 解题思路
|
||||
|
||||
sum 表示以当前元素结尾的连续子数组的最大和。前面的 sum 小于等于 0 时,接上它只会让和变小,不如从当前元素重新开始;否则把当前元素接在后面。所有位置的 sum 中最大的就是答案,时间复杂度 O(N),空间复杂度 O(1)。
|
||||
|
||||
```text
|
||||
val 6 -3 -2 7 -15 1 2 2
|
||||
sum 6 3 1 8 -7 1 3 5
|
||||
greatestSum 6 6 6 8 8 8 8 8
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSales(int[] sales) {
|
||||
if (sales == null || sales.length == 0)
|
||||
|
||||
Reference in New Issue
Block a user