[LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums
, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
53. Maximum Subarray 的变形,求和的时候,遇到0不会改变最大值,遇到负数也只是会减小最大值。而在求最大子数组乘积,遇到0会使整个乘积为0,遇到负数会使最大乘积变成最小乘积。
解法:DP,用2个dp数组分别记录到i时的最大乘积和最小乘积,因为下一个数字如果为负数就可以把最小的乘积是负的变成正的最大值。
Java:
1 2 3 4 5 6 7 8 9 10 11 | public int maxProduct( int [] A) { assert A.length > 0 ; int max = A[ 0 ], min = A[ 0 ], maxAns = A[ 0 ]; for ( int i = 1 ; i < A.length; i++) { int mx = max, mn = min; max = Math.max(Math.max(A[i], mx * A[i]), mn * A[i]); min = Math.min(Math.min(A[i], mx * A[i]), mn * A[i]); maxAns = Math.max(max, maxAns); } return maxAns; } |
Python:
1 2 3 4 5 6 7 8 9 | class Solution: # @param A, a list of integers # @return an integer def maxProduct( self , A): global_max, local_max, local_min = float ( "-inf" ), 1 , 1 for x in A: local_max, local_min = max (x, local_max * x, local_min * x), min (x, local_max * x, local_min * x) global_max = max (global_max, local_max) return global_max |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution2: # @param A, a list of integers # @return an integer def maxProduct( self , A): global_max, local_max, local_min = float ( "-inf" ), 1 , 1 for x in A: local_max = max ( 1 , local_max) if x > 0 : local_max, local_min = local_max * x, local_min * x else : local_max, local_min = local_min * x, local_max * x global_max = max (global_max, local_max) return global_max |
Python: wo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution( object ): def maxProduct( self , nums): """ :type nums: List[int] :rtype: int """ dp1 = [ 0 ] * len (nums) dp1[ 0 ] = nums[ 0 ] dp2 = [ 0 ] * len (nums) dp2[ 0 ] = nums[ 0 ] res = dp1[ 0 ] for i in xrange ( 1 , len (nums)): dp1[i] = max (dp1[i - 1 ] * nums[i], dp2[i - 1 ] * nums[i], nums[i]) dp2[i] = min (dp1[i - 1 ] * nums[i], dp2[i - 1 ] * nums[i], nums[i]) res = max (res, dp1[i]) return res |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution { public : int maxProduct(vector< int >& nums) { if (nums.empty()) return 0; int res = nums[0], mn = nums[0], mx = nums[0]; for ( int i = 1; i < nums.size(); ++i) { int tmax = mx, tmin = mn; mx = max(max(nums[i], tmax * nums[i]), tmin * nums[i]); mn = min(min(nums[i], tmax * nums[i]), tmin * nums[i]); res = max(res, mx); } return res; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution { public : int maxProduct(vector< int >& nums) { int res = nums[0], mx = res, mn = res; for ( int i = 1; i < nums.size(); ++i) { if (nums[i] > 0) { mx = max(mx * nums[i], nums[i]); mn = min(mn * nums[i], nums[i]); } else { int t = mx; mx = max(mn * nums[i], nums[i]); mn = min(t * nums[i], nums[i]); } res = max(res, mx); } return res; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution { public : int maxProduct(vector< int >& nums) { int res = nums[0], mx = res, mn = res; for ( int i = 1; i < nums.size(); ++i) { if (nums[i] < 0) swap(mx, mn); mx = max(nums[i], mx * nums[i]); mn = min(nums[i], mn * nums[i]); res = max(res, mx); } return res; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution { public : int maxProduct(vector< int >& nums) { int res = nums[0], prod = 1, n = nums.size(); for ( int i = 0; i < n; ++i) { res = max(res, prod *= nums[i]); if (nums[i] == 0) prod = 1; } prod = 1; for ( int i = n - 1; i >= 0; --i) { res = max(res, prod *= nums[i]); if (nums[i] == 0) prod = 1; } return res; } }; |
类似题目:
[LeetCode] 53. Maximum Subarray 最大子数组
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构