本文正在参加「Python主题月」,详情查看 活动连接html
这是 LeetCode 上的 剑指 Offer 42. 连续子数组的最大和 ,难度为 简单。git
Tag : 「线性 DP」github
输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求全部子数组的和的最大值。数组
要求时间复杂度为 。markdown
示例1:app
输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
复制代码
提示:oop
这是一道简单线性 DP 题。post
定义 为考虑以 为结尾的子数组的最大值。优化
不失通常性的考虑 如何转移。ui
显然对于 而言,以它为结尾的子数组分两种状况:
最终 为上述两种状况取 便可:
Java 代码:
class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
int[] f = new int[n];
f[0] = nums[0];
int ans = f[0];
for (int i = 1; i < n; i++) {
f[i] = Math.max(nums[i], f[i - 1] + nums[i]);
ans = Math.max(ans, f[i]);
}
return ans;
}
}
复制代码
Python 3 代码:
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
f = [0] * n
ans = f[0] = nums[0]
for i in range(1, n):
f[i] = max(nums[i], f[i - 1] + nums[i])
ans = max(ans, f[i])
return ans
复制代码
观察状态转移方程,咱们发现 明确值依赖于 。
所以咱们可使用「有限变量」或者「滚动数组」的方式,将空间优化至 。
Java 代码:
class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
int max = nums[0], ans = max;
for (int i = 1; i < n; i++) {
max = Math.max(nums[i], max + nums[i]);
ans = Math.max(ans, max);
}
return ans;
}
}
复制代码
class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
int[] f = new int[2];
f[0] = nums[0];
int ans = f[0];
for (int i = 1; i < n; i++) {
int a = i & 1, b = (i - 1) & 1;
f[a] = Math.max(nums[i], f[b] + nums[i]);
ans = Math.max(ans, f[a]);
}
return ans;
}
}
复制代码
Python 3 代码:
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
ans = curMax = nums[0]
for i in range(1, n):
curMax = max(nums[i], curMax + nums[i])
ans = max(ans, curMax)
return ans
复制代码
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
ans = nums[0]
f = [ans, 0]
for i in range(1, n):
a, b = i & 1, (i - 1) & 1
f[a] = max(nums[i], f[b] + nums[i])
ans = max(ans, f[a])
return ans
复制代码
一个有意思的拓展是,将 加法 替换成 乘法。
题目变成 152. 乘积最大子数组(中等)。
又该如何考虑呢?
一个朴素的想法,仍然是考虑定义 表明以 为结尾的最大值,但存在「负负得正」取得最大值的状况,光维护一个前缀最大值显然是不够的,咱们能够多引入一维 做为前缀最小值。
其他分析与本题同理。
Java 代码:
class Solution {
public int maxProduct(int[] nums) {
int n = nums.length;
int[] g = new int[n + 1]; // 考虑前 i 个,结果最小值
int[] f = new int[n + 1]; // 考虑前 i 个,结果最大值
g[0] = 1;
f[0] = 1;
int ans = nums[0];
for (int i = 1; i <= n; i++) {
int x = nums[i - 1];
g[i] = Math.min(x, Math.min(g[i - 1] * x, f[i - 1] * x));
f[i] = Math.max(x, Math.max(g[i - 1] * x, f[i - 1] * x));
ans = Math.max(ans, f[i]);
}
return ans;
}
}
复制代码
class Solution {
public int maxProduct(int[] nums) {
int n = nums.length;
int min = 1, max = 1;
int ans = nums[0];
for (int i = 1; i <= n; i++) {
int x = nums[i - 1];
int nmin = Math.min(x, Math.min(min * x, max * x));
int nmax = Math.max(x, Math.max(min * x, max * x));
min = nmin;
max = nmax;
ans = Math.max(ans, max);
}
return ans;
}
}
复制代码
Python 3 代码:
class Solution:
def maxProduct(self, nums: List[int]) -> int:
n = len(nums)
g = [0] * (n + 1) # 考虑前 i 个,结果最小值
f = [0] * (n + 1) # 考虑前 i 个,结果最大值
g[0] = f[0] = 1
ans = nums[0]
for i in range(1, n + 1):
x = nums[i - 1]
g[i] = min(x, min(g[i-1] * x, f[i-1] * x))
f[i] = max(x, max(g[i-1] * x, f[i-1] * x))
ans = max(ans, max(f[i], g[i]))
return ans
复制代码
这是咱们「刷穿 LeetCode」系列文章的第 No.剑指 Offer 42
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,咱们将先把全部不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路之外,还会尽量给出最为简洁的代码。若是涉及通解还会相应的代码模板。
为了方便各位同窗可以电脑上进行调试和提交代码,我创建了相关的仓库:github.com/SharingSour… 。
在仓库地址里,你能够看到系列文章的题解连接、系列文章的相应代码、LeetCode 原题连接和其余优选题解。