【Lintcode】191. Maximum Product Subarray
題目地址:
https://www.lintcode.com/problem/maximum-product-subarray/description
給定一個長 n n n的陣列 A A A,求其最大乘積子陣列,返回那個最大乘積。
思路是動態規劃。設 f [ i ] f[i] f[i]是以 A [ i ] A[i] A[i]結尾的最大乘積子陣列的乘積,那麼如果 A [ i ] ≥ 0 A[i]\ge 0 A[i]≥0,則 f [ i ] = max { A [ i ] , A [ i ] f [ i − 1 ] } f[i]=\max\{A[i],A[i]f[i-1]\} f[i]=max{A[i],A[i]f[i−1]},但如果 A [ i ] < 0 A[i]< 0 A[i]<0,那麼 f [ i ] f[i] f[i]應該是或者就是 A [ i ] A[i] A[i]本身,或者由 A [ i ] A[i] A[i]乘以以 A [ i − 1 ] A[i-1] A[i−1]為結尾的最小乘積子陣列的乘積得到,所以我們還需要開一個陣列 g g g, g [ i ] g[i] g[i]存以 A [ i ] A[i] A[i]結尾的最小乘積子陣列的乘積。這樣就有 { f [ i ] = max { A [ i ] , A [ i ] f [ i − 1 ] , A [ i ] g [ i − 1 ] } g [ i ] = min { A [ i ] , A [ i ] f [ i − 1 ] , A [ i ] g [ i − 1 ] } \begin{cases}f[i]=\max\{A[i],A[i]f[i-1],A[i]g[i-1]\}\\g[i]=\min\{A[i],A[i]f[i-1],A[i]g[i-1]\}\end{cases} {f[i]=max{A[i],A[i]f[i−1],A[i]g[i−1]}g[i]=min{A[i],A[i]f[i−1],A[i]g[i−1]}初始條件 f [ 0 ] = g [ 0 ] = A [ 0 ] f[0]=g[0]=A[0] f[0]=g[0]=A[0],答案就是 f f f和 g g g所有數字的最大值。程式碼如下:
public class Solution {
/**
* @param nums: An array of integers
* @return: An integer
*/
public int maxProduct(int[] nums) {
// write your code here
int res = Integer.MIN_VALUE;
int[] maxdp = new int[nums.length], mindp = new int[nums.length];
maxdp[0] = mindp[0] = nums[0];
res = nums[0];
for (int i = 1; i < nums.length; i++) {
int a = nums[i], b = nums[i] * maxdp[i - 1], c = nums[i] * mindp[i - 1];
maxdp[i] = Math.max(a, Math.max(b, c));
mindp[i] = Math.min(a, Math.min(b, c));
res = Math.max(res, Math.max(maxdp[i], mindp[i]));
}
return res;
}
}
時空複雜度 O ( n ) O(n) O(n)。
相關文章
- Leetcode Maximum Product SubarrayLeetCode
- Leetcode-Maximum Product SubarrayLeetCode
- LintCode-Maximum Subarray II
- LintCode-Maximum Subarray III
- LintCode-Maximum Subarray Difference
- [leetCode][001] Maximum Product SubarrayLeetCode
- LeetCode | 152. Maximum Product SubarrayLeetCode
- [LeetCode] Maximum Product Subarray 求連續子陣列的最大乘積LeetCode陣列
- Leetcode Maximum SubarrayLeetCode
- Leetcode-Maximum SubarrayLeetCode
- Maximum Subarray leetcode javaLeetCodeJava
- LintCode-Subarray Sum
- LintCode-Subarray Sum Closest
- LintCode-Minimum Subarray
- 最大子陣列問題(Maximum subarray problem)陣列
- Maximum Subarray 連續子陣列最大和陣列
- 53_Maximum Subarray-最大子陣列陣列
- LeetCode-Maximum Product of Word LengthsLeetCode
- LeetCode-Maximum Size Subarray Sum Equals kLeetCode
- [LeetCode] 2419. Longest Subarray With Maximum Bitwise ANDLeetCode
- 【Lintcode】1322. Product Equal B
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- 【Lintcode】1415. Residual Product
- leetCode(Using C)——718. Maximum Length of Repeated SubarrayLeetCode
- 【leetcode】53. Maximum Subarray 連續子序列的最大和LeetCode
- LeetCode C++ 1464. Maximum Product of Two Elements in an Array【Array/Sort】簡單LeetCodeC++
- 跟我一起刷leetCode演算法題11之 Maximum Product of Three NumbersLeetCode演算法
- Increase Subarray Sums
- Subarray Distinct Values
- [LeetCode] 191. Number of 1 BitsLeetCode
- Product Quantization
- A. Least ProductAST
- E - Maximum Glutton
- oracle 9i dataguard 由MAXIMUM PERFORMANCE模式變為MAXIMUM PROTECTIONOracleORM模式
- 二進位制陣列 subarray() 方法陣列
- [LeetCode] Minimum Size Subarray SumLeetCode
- Algorithm for Maximum Subsequence Sum zGo
- Cat, Fox and Maximum Array Split