【LeetCode】解题152:Maximum Product Subarray

5分钟搞定!用Coze+剪映小助手批量生成萌宠视频(附完整工作流配置) 本文详细介绍了如何利用Coze工作流与剪映小助手快速批量生成萌宠视频的完整方案。通过智能内容生成、素材处理、剪辑组装和输出适配四大模块的协同,实现5分钟内完成高质量视频制作。特别适合多宠物账号运营者,工作流支持模板切换和参数修改,大幅提升内容生产效率。 阅读详情

Problem 152: Maximum Product Subarray [Medium]

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.

来源:LeetCode

解题思路

使用动态规划思想。维护两个局部更新量max,min,当遇到负数时交换两个变量的值。同时维护一个全局更新量maxP,每次计算出新的max后更新一次maxP。

具体思路:

  • 首先将max,min,maxP都初始化为nums[0]。
  • 遍历nums,不断更新max,min,maxP,更新公式为:
    m a x = max ⁡ { m a x ∗ n u m s [ i ] , n u m s [ i ] } m i n = min ⁡ { m i n ∗ n u m s [ i ] , n u m s [ i ] } m a x P = max ⁡ { m a x P , m a x } max = \max\{max * nums[i], nums[i]\} \\ min = \min\{min * nums[i], nums[i]\} \\ maxP = \max\{maxP, max\} max=max{maxnums[i],nums[i]}min=min{minnums[i],nums[i]}maxP=max{maxP,max}
  • 遍历中如果遇到nums[i]为负数的情况,需要调换max,min的值之后再进行更新,因为乘以负数会将之前的最大值变为最小值,最小值变为最大值。
  • 最后返回maxP。

时间复杂度为 O ( n ) O(n) O(n),空间复杂度为 O ( 1 ) O(1) O(1)

运行结果:
在这里插入图片描述
要点:动态规划

Solution (Java)

class Solution {
    public int maxProduct(int[] nums) {
        int N = nums.length;
        if(N < 1) return 0;
        if(N < 2) return nums[0];
        int maxP = nums[0];
        int max = nums[0];
        int min = nums[0];
        for(int i = 1; i < N; i++){
            if(nums[i] < 0){
                int temp = max;
                max = min;
                min = temp;
            }
            max = Math.max(max * nums[i], nums[i]);
            min = Math.min(min * nums[i], nums[i]);
            maxP = Math.max(maxP, max);
        }
        return maxP;
    }
}
PCB 传输线教程(上):基于 Polar Si9000 与嘉立创工艺的传输线设计 理论线宽是W1,但在实际制作时,铜线顶部会有部分被刻蚀掉,表面被刻蚀后的宽度表示为W2,一般取经验值W2=W1-1,单位为 mil,Si9000 里面默认也是采用 W1,W2 相差 1 mil 的方式进行计算。表面阻抗结构就是简单的在一个平面上走信号线,以另外一个平面为地平面,信号线所在平面不进行铺铜(或铺铜间距大到可以忽略不计),这是最简单的一种结构,只需要知道介质层的厚度和介电常数、信号线厚度就可以进行阻抗计算。这个也是经验值,一般保持默认,或者可以填入 C1 相同的值(即忽略堆积)。 阅读详情

相关推荐

电流舵DAC设计(一)

如果分辨率继续增加,温度计码译码逻辑成为面积的主要决定因素,数字版图面积增加,总的面积消耗成上升趋势。缺点是:对于高分辨率的 DAC,高位码元和低位码元权重相差很大,当输入码元转换时(以 8 位二进制型 DAC 为例)尤其是中码转换(从 01111111 转换到 10000000),因为此时电流源的选通动作并非理想情况下的绝对一致等因素,导致在极短的时间内,码元会从 01111111 跳转到 11111111 再跳到10000000,这个过程中所有开关都参与转换,会引起最大幅度的尖峰毛刺(glitch);

模拟芯片设计 2827

Leetcode 152. Maximum Product Subarray

Leetcode 152. Maximum Product Subarray if there’s no zero in the array, then the subarray with maximum product must start with the first element or end with the last element. 例子: 关于0: [0, 100, 0] return 100; 关于负号: [-1, 2, 3] [1, 2, -3] [-1, 2, -3] class So

weixin_43876597的博客 156

一文解释电路中常用电子元器件应用和选型01

电容和二极管是电路中最常用的两个电子元器件,种类和系列众多,这里简单总结一下应用场景和选型方式 电容(Capacitor) 电容的种类(20种以上)和应用非常广泛,但是我经常遇到的无非就是下面这几种: 应用 1.隔直流。 2.旁路(去耦) 3.耦合 4.滤波 5.温度补偿 6.计时 7.调谐 8.整流 9.储能 种类 1. 陶瓷电容 2. 云母电容 3. 铝电解电容 4. 钽

weixin_43904073的博客 2214

LeetCode 152. Maximum Product Subarray 题解

本题是动态规划的经典问题,主要考察对状态定义和状态转移方程的理解。由于乘法的特殊性,我们需要同时跟踪当前的最大值和最小值。动态规划的核心思想是:通过遍历数组,同时维护当前的最大值和最小值,根据当前元素的正负性来更新这两个值,从而找到整个数组的最大乘积子数组。这种方法不仅适用于最大乘积子数组问题,还可以应用于许多其他需要同时跟踪最大值和最小值的问题。掌握动态规划的思想,对于解决这类问题非常重要。

cannonjinx的博客 2566

Leetcode 152 Maximum Product Subarray

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

weixin_43796689的博客 241

LeetCode-152. Maximum Product Subarray [C++][Java]

Given an integer arraynums, find a contiguous non-empty subarray within the array that has the largest product, and returnthe product.

贫道绝缘子的博客 524

Leetcode152 Maximum Product Subarray题解

Leetcode152 Maximum Product Subarray 原题 原题链接:https://leetcode.com/problems/maximum-product-subarray/ 原题大意: 给定一个整数数组 求乘积最大的子数组 思路 动态规划,理清其中的递推关系。 最大乘积可以在三种情况下出现: 1)若当前数大于0,到目前为止子数组大于0的最大的乘积(maxLast)乘以当前数字 2)若当前数字小于0,到目前为止子数组小于0的最小的乘积(minLast)乘以当前数字 3)

weixin_43573862的博客 207

152. Maximum Product Subarray

152. Maximum Product Subarray

hello world 236

[leetcode] 152. 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

小榕流光的专栏 705

Leetcode算法题之152. Maximum Product Subarray(Medium)【Python3题解】

题目描述: 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]

DaMoWangZQ的博客 325

LeetCode 152. Maximum Product Subarray 解题报告

LeetCode 152. Maximum Product Subarray 解题报告题目描述Find the contiguous subarray within an array (containing at least one number) which has the largest product. 示例Example 1: given the array [2,3,-2,4], th

骆小坑的填坑之旅 1809

LeetCode解题126:Word Ladder II(BFS算法

LeetCode解题 126:Word Ladder II(BFS算法)Problem 126: Word Ladder II [Hard]解题思路Solution (Java)修改过程 Problem 126: Word Ladder II [Hard] Given two words (beginWord and endWord), and a dictionary’s word list, ...

Fayedily的博客 771

LeetCode解题188:Best Time to Buy and Sell Stock IV(动态规划

LeetCode解题 188:Best Time to Buy and Sell Stock IV (动态规划)Problem 188: Best Time to Buy and Sell Stock IV [Hard]解题思路Solution (Java)修改过程 Problem 188: Best Time to Buy and Sell Stock IV [Hard] Say you hav...

Fayedily的博客 594

LeetCode解题123:Best Time to Buy and Sell Stock III(动态规划

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 487

LeetCode解题119:Pascal's Triangle II

LeetCode解题 118:Pascal's Triangle II Problem 118: Pascal's Triangle II [Easy]解题思路Solution (Java) Problem 118: Pascal’s Triangle II [Easy] Given a non-negative index k where k ≤ 33, return the kth index...

Fayedily的博客 444

LeetCode解题78:Subsets

LeetCode解题 78:Subsets Problem 78: Subsets [Medium]解题思路Solution (Java)修改过程 Problem 78: Subsets [Medium] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The so...

Fayedily的博客 443

LeetCode解题44:Wildcard Matching(动态规划解法)

LeetCode解题 44:Wildcard Matching(动态规划解法)Problem 44: Wildcard Matching [Hard]解题思路Solution (Java) Problem 44: Wildcard Matching [Hard] Given an input string (s) and a pattern (p), implement wildcard patt...

Fayedily的博客 429

LeetCode解题10:Regular Expression Matching(动态规划解法)

LeetCode解题 10:Regular Expression Matching(动态规划解法)Problem 10: Regular Expression Matching [Hard]解题思路Solution (Java) Problem 10: Regular Expression Matching [Hard] Given an input string (s) and a patter...

Fayedily的博客 411

LeetCode解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 366

LeetCode解题31:Next Permutation

LeetCode解题 31:Next Permutation Problem 31: Next Permutation [Medium]解题思路Solution (Java)修改过程 Problem 31: Next Permutation [Medium] Implement next permutation, which rearranges numbers into the lexicogr...

Fayedily的博客 352

LeetCode解题32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)

LeetCode解题 32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)Problem 32: Longest Valid Parentheses [Hard]解题思路1. 栈2. 动态规划3. 计数器Solution (Java) Problem 32: Longest Valid Parentheses [Hard] Given a string cont...

Fayedily的博客 352

LeetCode解题714:Best Time to Buy and Sell Stock with Transaction Fee(动态规划

LeetCode解题 714:Best Time to Buy and Sell Stock with Transaction Fee (动态规划)Problem 714: Best Time to Buy and Sell Stock with Transaction Fee [Medium]解题思路Solution (Java) Problem 714: Best Time to Buy an...

Fayedily的博客 339

qualityguide_质量图引导的相位解包方法_质量图_

基于质量引导的相位解包方法,包括质量图的计算,质量引导的洪水填充解包

基于 BW6101芯片 的超级电容均压板AD设计硬件原理图+PCB文件.zip

基于 BW6101芯片 的超级电容均压板AD设计硬件原理图+PCB文件,硬件采用2层板设计,大小为100x45mm,Altium Designer 设计的工程文件,包括完整的原理图及PCB文件,可以用Altium(AD)软件打开或修改,可作为你产品设计的参考。

上一篇: 【LeetCode】解题128:Longest Consecutive Sequence
下一篇: 【LeetCode】解题5:Longest Palindromic Substring(多解法:动态规划+中心扩散)
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值