首页 > 其他分享 >152. Maximum Product Subarray

152. Maximum Product Subarray

时间:2022-11-20 23:24:41浏览次数:40  
标签:Product 152 return nums int maxsofar product Subarray Math

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.

 

Example 1:

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

Example 2:

Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

 

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

 

//要考虑特殊情况,即负数和负数相乘,如2,-3,-7,所以在处理乘法时,除了维护一个局部最大值,还要维护一个局部最小值。

class Solution {

    public int maxProduct(int[] nums) {

       if (nums.length == 0)

          return 0;

       if (nums.length == 1)

          return nums[0];

       int maxsofar = nums[0];

       int maxherepre = nums[0];

       int minherepre = nums[0];

       int maxhere, minhere;

       for (int i = 1; i < nums.length; i++) {

          maxhere = Math.max(Math.max(maxherepre * nums[i], minherepre * nums[i]), nums[i]);

          minhere = Math.min(Math.min(maxherepre * nums[i], minherepre * nums[i]), nums[i]);

          maxsofar = Math.max(maxhere, maxsofar);

          maxherepre = maxhere;

          minherepre = minhere;

       }

       return maxsofar;

   }

}

分享:  

标签:Product,152,return,nums,int,maxsofar,product,Subarray,Math
From: https://www.cnblogs.com/MarkLeeBYR/p/16910024.html

相关文章

  • F - Subarrays题解
    F-Subarrays题意:给你一个序列,问这个序列里有多少个子串的和能被k整除。思路:求前缀和,然后每个位置对k取模,模数相等的位置之间,是一个满足条件的字串。因为求的是前缀和,......
  • CF631E Product Sum
    前言不知道为什么题解里的李超线段树都要分两种情况讨论,我觉得的大可不必,其实一遍就好了。分析设\(ans_i\)为\(i\)移动到其他位置时获得的最大取值,\(sum_i\)为\(......
  • 使用MapStruct出现了No property named "productId" exists in source parameter(s).
    pom.xml<properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.buil......
  • 【做题笔记】CF1528B Kavi on Pairing Duty
    ProblemCF1528BKavionPairingDuty题目大意:在数轴上有\(2n\)个点,相邻两个点的距离为\(1\)。现在要将这些点两两匹配成\(n\)个圆弧,要求任意两个圆弧要么等长,要么......
  • dp-leetcode152
    动态规划问题,存在重叠子问题/***<p>给你一个整数数组<code>nums</code> ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘......
  • MBR15200FAC-ASEMI插件肖特基二极管MBR15200FAC
    编辑-ZMBR15200FAC在ITO-220AC封装里采用的1个芯片,其尺寸都是95MIL,是一款插件肖特基二极管。MBR15200FAC的浪涌电流Ifsm为175A,漏电流(Ir)为0.05mA,其工作时耐温度范围为-65......
  • ASEMI肖特基二极管MBR15200AC特征,MBR15200AC机械数据
    编辑-ZASEMI肖特基二极管MBR15200AC参数:型号:MBR15200AC最大重复峰值反向电压(VRRM):200V最大RMS电桥输入电压(VRMS):140V最大直流阻断电压(VDC):200V最大平均正向整流输出电流......
  • 力扣-152-乘积最大的子数组
    对于dp[i],如果nums[i-1]>0,dp[i-1]也>0,那就是dp[i-1]*nums[i-1]如果<0,>0,那就是nums[i-1]0,<0,那就是nums[i-1]<0,<0,那就是dp[i-1]*nums[i-1]同样参考最大子数和,还需......
  • Leetcode第1668题:最大重复子字符串(Maximum repeating subarray)
    解题思路题目条件字符串长度不超过100,直接从大到小枚举word的重复次数k,判断word重复次数后是否还是sequence的字串,是就直接返回重复次数k。核心代码如下:classSolution......
  • CF487C Prefix Product Sequence
    CF487CPrefixProductSequence一道妙哉的构造题。首先有两点很明显:\(1\)一定在第一个,\(n\)一定在最后一个。除了\(4\)的合数都无解(\(1\)特判)根据题解第......