Files
2020-08-07 17:06:53 +08:00

28 lines
974 B
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
## 题目
Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
**Example:**
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
**Follow up:**
If you have figured out the O(*n*) solution, try coding another solution using the divide and conquer approach, which is more subtle.
## 题目大意
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
## 解题思路
- 这一题可以用 DP 求解也可以不用 DP。
- 题目要求输出数组中某个区间内数字之和最大的那个值。`dp[i]` 表示 `[0,i]` 区间内各个子区间和的最大值,状态转移方程是 `dp[i] = nums[i] + dp[i-1] (dp[i-1] > 0)``dp[i] = nums[i] (dp[i-1] ≤ 0)`