【LeetCode】解题152:Maximum Product Subarray

yolov5部署之七步完成tensorRT模型推理加速 目录前言step 1. 下载yolov5源码以及tensorrtx源码,并将yolov5s.pt转为.wts模型step 2. 确定并下载自己准备使用的CUDA版本。step 3. 根据自己的CUDA版本下载对应版本的cuDNN以及TensorRTstep 4. 根据自己使用的VS版本下载对应版本的OpenCVstep 5. 安装CUDAstep 6. 安装cuDNN、TensorRT以及验证TensorRT是否安装成功 前言     前段时间研究了Pytorch的环境配置,之后便从github上下载了yo 阅读详情

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{max∗nums[i],nums[i]}min=min{min∗nums[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;
    }
}
2022年灌溉试验设计与数据分析考试试题 2.表2数据为表层0~10cm的原状土离心机土壤水分特征曲线试验数据,(1)请利用R语言nls函数通过Van Genuchten方程对土壤体积含水率和土壤基质势进行拟合;(3)指明土壤基值势为-33 kPa和-1500kPa时的体积含水率。1.请根据表1数据,以灌溉模式为主区,生物炭用量为子区,利用R语言agricolae包生成裂区试验设计试验处理布置图;3.以R工具包factoextra中decathlon2数据集前10列为数据进行PCA分析,要求计算出(1)前5个主成分的特征值、特征向量; 阅读详情

相关推荐

MATLAB联合Fluent优化仿真

matlab与fluent交互

keleke_的博客 4734

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的博客 162

遥感生态指数(RSEI)——四个指数的计算

遥感生态指数是在4个生态指数的基础上通过主成分变换得出的一个评价生态环境质量的指标。因此,第二部分就是要分别计算出这4个指数的值。 ①首先,是绿度指数的计算,一般用归一化植被指数(NDVI) NDVI=(NIR-Red)/(NIR+Red) 在Bandmath工具中输入NDVI计算公式 (float(b4-b3))/(b4+b3) 其中,b3、b4分别是红波段和近红外波段 ②其次,是湿度计算。 WET = c1B1 + c2B2 + c3B3...

qq_41978498的博客 9万+

LeetCode 152. Maximum Product Subarray 题解

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

cannonjinx的博客 2804

Android13 SurfaceFlinger启动流程分析

在Android13版本中,SurfaceFlinger是由Android.bp去启动init.rc文件,然后再解析文件去加载SurfaceFlinger。上面方法主要处理如下:1、调用surfaceflinger的createSurfaceFlinger方法,实例化surfaceflinger。2、调用flinger(surfaceflinger)的init()方法,在客户端连接之前进行初始化。

liuning1985622的博客 1924

Visual Studio Code ADT Extension 能不能连接 ECC,答案是可以,但真正的门槛在 ABAP 后端版本

Visual Studio Code 版 ABAP Development Tools(ADT)支持连接传统 SAP ECC 系统,关键不在于系统名称,而在于底层 SAP NetWeaver AS ABAP 版本是否达到最低要求(7.31 SP04 及以上)。只要系统已支持 Eclipse ADT,即可无缝使用 VS Code ADT,无需升级后端。SAP 通过复用原有 ADT 技术栈,将兼容性逻辑置于客户端,实现对老系统的持续支持。尽管初期侧重 ABAP Cloud 模型,但 1.1 及后续版本已全面覆盖

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 73

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的博客 246

【数据集】MODIS 的 MOD07 大气廓线产品概述及下载

MODIS 的 MOD07 大气廓线产品概述及下载

WW、forever的博客 554

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.

贫道绝缘子的博客 525

ENS160传感器原理图设计,已量产(空气质量传感器)

ENS160 是一款集成式数字空气质量传感器,核心优势源于 “MEMS 传感元件 + 内置微处理器” 的架构。

不脱发的程序猿 584

315、弱电基础知识介绍

常见的弱电系统包括:闭路电视监控系统、防盗报警系统、门禁系统、电子巡更系统、停车场管理系统、可视对讲系统、家庭智能化系统及安防系统、背景音乐系 统、LED显示系统、等离子拼接屏系统、DLP大屏系统、三表抄送系统、楼宇自控系统、防雷与接地系统、寻呼对讲及专业对讲系统、弱电管道系统、UPS不 间断电源系统、机房系统、综合布线系统、计算机局域网系统、物业管理系统、多功能会议室系统、有线电视系统、卫星电视系统、卫星通讯系统、消防系统、电话 通讯系统、酒店管理系统、视频点播系统、人力资源管理系统等等。

qq_43416206的博客 1380

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的博客 210

一文讲清产品开发流程:IPD、门径管理、敏捷开发到底有什么区别?

夜视清晰人形识别异常声音提醒App 远程查看本地隐私保护研发开始评估芯片算力、摄像头模组、功耗、散热、算法可行性。让产品开发更少浪费、更少返工、更快验证、更高质量、更接近客户真实需求。IPD:让公司端到端协同,把产品做成。门径管理:在关键节点做决策,把资源投对。敏捷开发:用短周期迭代,把变化接住。精益开发:减少等待和返工,把效率做高。对新人来说,不必一开始就背很多术语。产品开发不是从画图、写代码、做样机开始,而是从理解客户、定义价值、控制风险、持续验证开始。

qq_52293640的博客 71

152. Maximum Product Subarray

152. Maximum Product Subarray

hello world 241

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

[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

小榕流光的专栏 706

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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的博客 333

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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

骆小坑的填坑之旅 1814

【LeetCode】#152乘积最大子序列(Maximum Product Subarray)

【LeetCode】#152乘积最大子序列(Maximum Product Subarray) 题目描述 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。 示例 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6。 示例 2: 输入: [-2,0,-1] 输出: 0 解释: 结果不能为 2, 因为 [-2,-1...

weixin_43858604的博客 179

【Leetcode】152. Maximum Product Subarray

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] Out...

蒋志碧的博客 423

Leetcode 152 Maximum Product Subarray 子数组最大乘积

在数组(至少包含一个元素)中找到一个连续的子数组,使得该子数组元素的乘积最大。

(ง •̀_•́)ง 1065

LeetCode - 解题笔记 - 152 - Maximum Product Subarray

Solution 1 0053. Maximum Subarray 的变体,同样是动态规划,最优子结构表示以当前位置结尾的最优区间结果。但是需要额外注意负负的整的情况,如果当前数值为负数,那么实际最大结果应该来自前一轮计算的最小的候选,因此需要同时维护两个状态,一个记录最大候选,一个记录最小候选,最优结果取当前轮最大结果。 时间复杂度:O(n)O(n)O(n),其中nnn为输入的元素个数,线性遍历一次。 空间复杂度:O(1)O(1)O(1),可以优化到只维护常数个状态量。 class Solution

支锦铭的博客 1674

【Java】【LeetCode】152. Maximum Product Subarray

题目: Given an integer arraynums, 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:[...

zhelong19830726的博客 230

【LeetCode】152. Maximum Product Subarray 乘积最大子数组(Medium)(JAVA)

【LeetCode】152. Maximum Product Subarray 乘积最大子数组(Medium)(JAVA) 题目地址: https://leetcode.com/problems/maximum-product-subarray/ 题目描述: Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the

qq_16927853的博客 228

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

Leetcode 152.乘积最大子序列 1 题目描述(Leetcode题目链接)   给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6。 输入: [-2,0,-1] 输出: 0 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。 2 题解   一开始的想法...

qq_39378221的博客 207

LeetCode—152.乘积最大子序列(Maximum Product Subarray)——分析及代码(C++)

LeetCode—152.乘积最大子序列[Maximum Product Subarray]——分析及代码[C++] 一、题目 二、分析及代码 1. 动态规划(优化后) (1)思路 (2)代码 (3)结果 2. 分类讨论(优化前) (1)思路 (2)代码 (3)结果 三、其他 一、题目 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6。 示例 2: 输入: [-2

江南土豆的博客 287

leetcode----152. Maximum Product Subarray

链接: https://leetcode.com/problems/maximum-product-subarray/ 大意: 给定一个整型数组nums,数组中元素可正可负可为0.要求找出一个连续子数组,使得该子数组是所有子数组中各元素最大,返回最大的乘积。例子: 思路: 一开始感觉应该用动态规划来做,但是递推方程式始终没想出来。于是只能老老实实地做了... 首先使用一个列表 ze...

smart_ferry的博客 300
上一篇: 【LeetCode】解题128:Longest Consecutive Sequence
下一篇: 【LeetCode】解题5:Longest Palindromic Substring(多解法:动态规划+中心扩散)
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值