首页 > 其他分享 >152- Maximum Produce Subarray-最大子数组之乘积

152- Maximum Produce Subarray-最大子数组之乘积

时间:2024-05-19 22:19:08浏览次数:23  
标签:152 乘积 nums int dpMax dpMin Maximum Produce 数组

问题描述

Given an integer array nums, find a subarray .that has the largest product, and return the product.
The test cases are generated so that the answer will fit in a 32-bit integer.

解释: 找出一个数组中乘积最大的子数组,返回子数组的乘积。

案例:

Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.

解释: 上述给定数组中,最大乘积子数组是6.

基本思想

这是一个典型的两个动态数组相互更新的案例。
设定两个数组 \(dpMax\),\(dpMin\), 其中

  • \(dpMax[i]\) 表示以\(nums[i]\)为结尾的子数组的最大乘积;
  • \(dpMin[i]\) 表示以\(nums[i]\)为结尾的子数组的最小乘积;
    则 相应更新公式如下:
  • \(dpMax[i]\) = max( \(dpMax[i]\)*\(nums[i-1]\) , \(dpMin[i]\) * \(nums[i-1]\), \(nums[i-1]\))
  • \(dpMin[i]\) = min( \(dpMin[i]\)*\(nums[i-1]\) , \(dpMin[i]\) * \(nums[i-1]\), \(nums[i-1]\))

时间复杂度\(O(n))\)

代码

c++

    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        if (n<=1) return nums[0];
        vector<int> dpMax(n,0);// 记录以nums[i]为结尾的最大子数组乘积
        vector<int> dpMin(n,0);// 记录以nums[i]为结尾的最小子数组乘积
        dpMax[0] = nums[0];
        dpMin[0] = nums[0];
        int res = nums[0];
        for(int i=1;i<n;++i) {
           dpMax[i] = max(max(dpMax[i-1] * nums[i], dpMin[i-1] * nums[i]), nums[i]);
           dpMin[i] = min(min(dpMax[i-1] * nums[i], dpMin[i-1] * nums[i]), nums[i]);
           res = max(res, dpMax[i]);
        }
        return res;
    }

python

   def maxProduct(self, nums: List[int]) -> int:
        n = len(nums)
        if n <=0: return 0
        if n<=1 : return nums[0]
        dpMax = [0] * n
        dpMin = [0] * n
        dpMax[0] = nums[0]
        dpMin[0] = nums[0]
        res = nums[0]
        for i in range(1, n):
             dpMax[i] = max(max(dpMax[i-1] * nums[i], dpMin[i-1] * nums[i]), nums[i])
             dpMin[i] = min(min(dpMax[i-1] * nums[i], dpMin[i-1] * nums[i]), nums[i])
             res = max(dpMax[i], res)
        return res

标签:152,乘积,nums,int,dpMax,dpMin,Maximum,Produce,数组
From: https://www.cnblogs.com/douniwanli/p/18200847

相关文章

  • 152. 乘积最大子数组
    给你一个整数数组nums,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。测试用例的答案是一个32-位整数。示例1:输入:nums=[2,3,-2,4]输出:6解释:子数组[2,3]有最大乘积6。示例2:输入:nums=[-2,0,-1]输......
  • LeetCode 1353. Maximum Number of Events That Can Be Attended
    原题链接在这里:https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/description/题目:Youaregivenanarrayof events where events[i]=[startDayi,endDayi].Everyevent i startsat startDayi andendsat endDayi.Youcanattend......
  • LeetCode 918. Maximum Sum Circular Subarray
    原题链接在这里:https://leetcode.com/problems/maximum-sum-circular-subarray/description/题目:Givena circularintegerarray nums oflength n,return themaximumpossiblesumofanon-empty subarray of nums.A circulararray meanstheendofthearray......
  • skipped: maximum number of running instances reached (1)
    Python的 apscheduler今天出现skipped:maximumnumberofrunninginstancesreached(1)问题产生的原因:设置了大量的任务,而APScheduler无法同时处理所有任务解决方法:调整APScheduler使用的线程池大小来增加并发处理任务的能力fromapscheduler.schedulers......
  • LeetCode 3009. Maximum Number of Intersections on the Chart
    原题链接在这里:https://leetcode.com/problems/maximum-number-of-intersections-on-the-chart/description/题目:Thereisalinechartconsistingof n pointsconnectedbylinesegments.Youaregivena 1-indexed integerarray y.The kth pointhascoordinates......
  • 53_Maximum Subarray-最大子数组
    问题描述Givenanintegerarray nums,findthe subarray withthelargestsum,andreturn itssum.给定一个数组nums,找到一个子数组。使它的和最大,返回子数组例子Input:nums=[-2,1,-3,4,-1,2,1,-5,4]Output:6Explanation:子数组[4,-1,2,1]有最大的和6.基......
  • LeetCode 1186. Maximum Subarray Sum with One Deletion
    原题链接在这里:https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/description/题目:Givenanarrayofintegers,returnthemaximumsumfora non-empty subarray(contiguouselements)withatmostoneelementdeletion. Inotherwords,youwa......
  • AP5152 是一种输出电流可调的、低压差的 LED 恒流驱动器
    AP5152是一种输出电流可调的、低压差的LED恒流驱动器,仅需一个外接电阻和一个NMOS管就可以构成一个完整的LED恒流驱动电路,调节该外接电阻就可以调节输出电流,输出电流可调范围为100mA到3000mA。AP5152内置过热保护功能,可有效保护芯片,避免温度超过120oC时因过热而造成损......
  • LeetCode 1373. Maximum Sum BST in Binary Tree
    原题链接在这里:https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/description/题目:Givena binarytree root,return themaximumsumofallkeysof any sub-treewhichisalsoaBinarySearchTree(BST).AssumeaBSTisdefinedasfollows:Thel......
  • P1525 [NOIP2010 提高组] 关押罪犯
    原题链接题解这题我采用了带权并查集的做法,0代表两囚犯处于监狱,1代表两囚犯不同监狱。根据题意,我们想让冲突值尽可能的小,那么我们要先把仇恨值大的两罪犯放在不同监狱;即按仇恨值从大到小的去判断每条仇恨信息。(贪心思想)code #include<bits/stdc++.h>usingnamespacestd;......