leetcode 之Maximum Product Subarray

题目描述:

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

Subscribe to see which companies asked this question

即: 给定一个数组, 计算最大的连续乘积。

如: 给定[2,3,-2,4] 时,最大乘积为2*3 = 6

  给定[-2, 3, -4] 时, 最大乘积为 -2*3*-4

因此,因此遇到负数时,不能简单认为是最小的,如果后面还有负数的话,会使计算错误。

以第i个数结尾的最大的乘积计算如下:

1. 如果第i个数大于0, 则max[i] = max(max[i-1]*nums[i], nums[i])

2. 如果第i个数小于0, 则max[i]  = min(min[i-1]*nums[i], nums[i])

因此我们需要记录 以第i个数结尾的最大和最小乘积。

以第i个数结尾的最小乘积计算如下:

1. 如果nums[i] 大于0, 则min[i] = min(min[i-1]*nums[i], nums[i])

2. 如果nums[i] 小于0, 则min[i] = max(max[i-1]*nums[i], nums[i])

代码如下:

 1 class Solution(object):
 2     def maxProduct(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         ln = len(nums)
 8         maxPro = [0]*(ln+1)
 9         minPro = [0]*(ln+1)
10         maxPro[0] = 1
11         minPro[0] = 1
12         for i in range(0, ln):
13             if nums[i] >= 0:
14                 maxPro[i+1] = max(maxPro[i]*nums[i], nums[i])
15                 minPro[i+1] = min(minPro[i]*nums[i], nums[i])
16             else:
17                 minPro[i+1] = min(maxPro[i]*nums[i], nums[i])
18                 maxPro[i+1] = max(minPro[i]*nums[i], nums[i])
19         return max(maxPro[1:])
时间: 2024-11-06 09:47:59

leetcode 之Maximum Product Subarray的相关文章

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 

LeetCode:Maximum Product Subarray

题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这道题属于动态规划的题型,

[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]

[C++]LeetCode: 96 Maximum Product Subarray(动态规划)

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 思路: 这道题和Maximum Subarray解法类似,维护两个变量来动

LeetCode 152. Maximum Product Subarray (最大乘积子数组)

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 题目标签:Array, Dynamic Programming 题目给了我们一个nu

LeetCode – Refresh – Maximum Product Subarray

Similar to maximum sum subarray, but need a gmin to record the global minimum to handle negative number multiplication. 1 class Solution { 2 public: 3 int maxProduct(int A[], int n) { 4 int result = A[0], gMax = A[0], gMin = A[0]; 5 for (int i = 1; i

【Leetcode】Maximum Product Subarray

题目链接:https://leetcode.com/problems/maximum-product-subarray/ 题目: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has t

leetcode 152. Maximum Product Subarray --------- java

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 找出最大的相乘的数字,很简单,代码还可以优化的地方很多.但是速度还可以. publi

C#解leetcode 152. Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 分析:这个题目就是让你求连续的子数组的乘积的最大值. (1)在数组中没有0的情况下,