【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 | 152. Maximum Product SubarrayLeetCode
- [LeetCode] 2419. Longest Subarray With Maximum Bitwise ANDLeetCode
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- 【Lintcode】1415. Residual Product
- 318-Maximum Product of Word Lengths
- [LeetCode] 2841. Maximum Sum of Almost Unique SubarrayLeetCode
- 【Lintcode】1322. Product Equal B
- 最大子陣列問題(Maximum subarray problem)陣列
- 53_Maximum Subarray-最大子陣列陣列
- 【leetcode】53. Maximum Subarray 連續子序列的最大和LeetCode
- LeetCode C++ 1464. Maximum Product of Two Elements in an Array【Array/Sort】簡單LeetCodeC++
- 一道題把我氣笑了:) 力扣.53 最大子陣列和 leetcode maximum-subarray力扣陣列LeetCode
- [LeetCode] 191. Number of 1 BitsLeetCode
- Increase Subarray Sums
- Subarray Distinct Values
- Product Quantization
- [LeetCode] 523. Continuous Subarray SumLeetCode
- A. Least ProductAST
- E - Maximum Glutton
- [LeetCode] 560. Subarray Sum Equals KLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- LeetCode: 560. Subarray Sum Equals KLeetCode
- LeetCode 209. Minimum Size Subarray Sum ?LeetCode
- Leetcode 209. Minimum Size Subarray SumLeetCode
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- [LeetCode] Third Maximum NumberLeetCode
- 二進位制陣列 subarray() 方法陣列
- LeetCode 974 Subarray Sums Divisible by K All In OneLeetCode
- ABC 322 E Product Developmentdev
- SAP EPD - Enterprise Product Developmentdev
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- Lintcode 1263. Is Subsequence
- 【Lintcode】1189. Minesweeper
- 654-Maximum Binary Tree
- 1383. Maximum Performance of a TeamORM
- Cat, Fox and Maximum Array Split
- 238. Product of Array Except Self