mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
28 lines
974 B
Markdown
Executable File
28 lines
974 B
Markdown
Executable File
# [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)`。
|