[leetcode] 152. Maximum Product Subarray

OpenCV图像去畸变实战指南:从原理到参数调优(附完整代码与效果对比) 本文详细解析了OpenCV图像去畸变的原理与实战方法,对比了三种核心函数(标准、鱼眼、全景模型)的适用场景与效果。重点提供了完整的代码示例,并深入探讨了alpha、焦距、中心点等关键参数的调优技巧,以帮助开发者有效矫正镜头畸变,提升计算机视觉任务精度。 阅读详情

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]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

解法1

构建两个动态数组,f[i]记录子数组[0, i]范围内并且一定包含nums[i]数字的最大子数组乘积,g[i]表示子数组[0, i]范围内并且一定包含nums[i]数字的最小子数组乘积。
那么遍历到nums[i]时,最大值和最小值一定在f[i-1]*nums[i],g[i-1]*nums[i],和nums[i]中产生。

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n,0), g(n,0);
        f[0] = g[0] = nums[0];
        int res = f[0];
        for(int i=1;i<n;i++){
            f[i] = max(max(f[i-1]*nums[i], g[i-1]*nums[i]), nums[i]);
            g[i] = min(min(f[i-1]*nums[i], g[i-1]*nums[i]), nums[i]);
            res = max(res, f[i]);
        }
        return res;
    }
};

解法2

不用构建新数组来记录,直接使用两个变量mx和mn分别记录当前最大值和当前最小值。

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        int mx = nums[0],mn = nums[0];
        int res = nums[0];
        for(int i=1;i<n;i++){
            int tmax = mx, tmin = mn;
            mx = max(max(tmax*nums[i], tmin*nums[i]), nums[i]);
            mn = min(min(tmax*nums[i], tmin*nums[i]), nums[i]);
            res = max(mx, res);
        }
        return res;
    }
};

解法3

先从前往后扫一遍,再从后往前扫一遍,遇到0就把p=1.

    int maxProduct(vector<int>& nums) {
        int res = nums[0], prod = 1, n = nums.size();
        for (int i = 0; i < n; ++i) {
            res = max(res, prod *= nums[i]);
            if (nums[i] == 0) prod = 1;
        }
        prod = 1;
        for (int i = n - 1; i >= 0; --i) {
            res = max(res, prod *= nums[i]);
            if (nums[i] == 0) prod = 1;
        }
        return res;
    }

参考

https://www.cnblogs.com/grandyang/p/4028713.html

Mamba实战:如何用Samba框架轻松搞定RGB-D显著性检测(附避坑指南) 本文提供了基于Samba框架进行RGB-D显著性目标检测的实战指南。详细解析了从环境配置、数据预处理到模型推理与可视化的全流程,并重点剖析了多模态数据融合中的常见陷阱与调试技巧,旨在帮助开发者高效部署这一基于Mamba的先进视觉模型,规避工程实践中的典型问题。 阅读详情

相关推荐

逆向分析美团外卖mtgsig3.0签名算法:从抓包到算法还原实战

在移动应用开发与安全领域,API接口签名是保障数据传输完整性与身份验证的核心机制。其基本原理是通过特定的算法,将请求参数、时间戳等数据与密钥结合,生成唯一的签名串,服务器通过验证签名来识别请求的合法性。这项技术的核心价值在于防止数据篡改、重放攻击和未授权访问,是构建安全客户端-服务器通信的基石。在实际应用中,签名算法广泛应用于电商、社交、金融等各类App的接口保护场景。本文以美团外卖App的mtgsig3.0签名为具体案例,详细拆解了其生成逻辑。通过结合Frida动态调试与Jadx静态分析,文章深入探讨了如

weixin_29325515的博客 1245

LeetCode 53/152 Maximum Subarray/Maximum Product Subarray(动态规划)

LeetCode 53/152 Maximum Subarray/Maximum Product Subarray(动态规划)

花事未了_喃喃语 436

CA51F3系列(国产)+低功耗模式

CA51F351P6芯片是基于1T 8051内核的8位微控制器,通常情况下,运行速度比传统的8051芯片快10倍,性能更加优越。内置8K Flash程序存储器,可多次重复编程的特性给用户开发带来了极大的方便。不仅保留了传统8051芯片的基本特性,还集成了12Bit ADC、LCD/LED驱动、Touch Key、16Bit PWM、 UART、RTC、低电压检测(LVD)等功能模块。支持IDLE、STOP和低速运行三种省电模式以适应不同功耗要求的应用。

WANGJIHUIwang的博客 572

LeetCode 152. Maximum Product Subarray(动态规划)

题目来源:https://leetcode.com/problems/maximum-product-subarray/ 问题描述 152.Maximum Product Subarray Medium Given an integer arraynums, find the contiguous subarray within an array (containing at leas...

da_kao_la的博客 308

保姆级教程:将YOLOv8模型封装成Docker镜像,集成到CVAT实现自动标注

本文提供了一份详细的保姆级教程,指导如何将YOLOv8模型封装成Docker镜像并集成到CVAT平台,实现自动化标注。通过环境配置、模型服务化封装、Docker镜像构建与优化、CVAT平台集成等步骤,帮助开发者高效完成计算机视觉项目的自动标注工作流,显著提升标注效率。

weixin_28692817的博客 414

NCBI|叶绿体基因组数据上传

叶绿体基因组NCBI上传 小编最近在NCBI上传了好几次数据…想着可以把具体的上传步骤写下来,供大家参考。以下是叶绿体基因组的上传示例,大家可以瞅瞅 图片 准备工作 1.登录NCBI官网:https://www.ncbi.nlm.nih.gov/, 依次点击“sign in to NCBI”——“See more 3rd party sign in options”——搜索“Microsoft live.com”。 ”sign in to NCBI“ ”See more 3rd party sign i

weifanbio的博客 3901

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

西门子1200数据块BD详解

定义 DB(Data Block)是用于存储用户数据的区域,分为两种类型:核心作用 四、DB块的访问方式 1. 全局DB访问 符号访问(优化块): 绝对地址访问(非优化块):(假设SetPoint占用2字节) 2. 背景DB访问 与FB关联后自动生成,通过FB调用时传递: 五、高级功能与注意事项 数据块的初始化运行时修改DB数据块的下载常见问题 通过合理使用DB块,可显著提升程序的可读性和可维护性。建议优先启用优化块访问并使用符号名编程。如何使用DB数据块 数据块用于存储程序数据。因此,数据块

weixin_51020122的博客 3429

Leetcode152 Maximum Product Subarray python

Leetcode152 Maximum Product Subarray题目解法1:brutal force解法2:同时记录最小和最大值解法3:分类讨论解法4:双向动规 题目 Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which...

qq_37821701的博客 310

信创环境下,软件测试如何破局?

毕竟,在自主可控的道路上,每一行通过测试的代码,都在为信创产业的未来添砖加瓦。未来,测试工程师不仅要验证软件在信创环境的可用性,更要基于国产软硬件的特性,推动软件进行 “原生设计”—— 例如,针对国产 CPU 的多核架构优化代码,基于国产数据库的索引机制提升查询效率。某政务云平台的测试数据显示,在 “飞腾芯片 + 麒麟操作系统” 环境下,系统的并发处理能力较传统环境下降约 15%,测试团队通过优化软件的线程调度逻辑,最终将性能损失控制在 5% 以内 —— 这正是信创性能测试的价值所在。

weixin_45608163的博客 885

OpenAI最强编程助手Codex:下载安装、使用指南(含使用方式、提示技巧、趋势)

OpenAI最近上线了最强编程Agent——Codex,一起来看看吧!

Code_流苏:在代码中寻诗意,在实践中觅真知 1万+

LeetCode 152. Maximum Product Subarray 题解

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

cannonjinx的博客 2802

harbor x509: certificate signed by unknown authority【20251222】

本教程通过「合并证书链(服务器端)+ 预置根 CA(客户端)」彻底解决了自签名证书的 x509 错误,同时集成了 Trivy 漏洞扫描,实现了 Harbor 从搭建到实用的全流程。证书链合并是服务器端的关键,确保客户端获取完整信任路径;客户端需同时配置「系统信任库 + Docker 信任目录」,避免 Docker 独立信任机制导致的问题;所有 IP 需保持一致(Harbor 配置、证书绑定、客户端访问),是避免错误的基础。

老韩的Linux云计算架构师进阶之路 681

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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[leetcode] 152 Maximum Product Subarray

先来一个比较简单的题目,也是出现较多的,求连续的最大子数组和,见hdu 1003. 比较简单的动态规划,如果当前的连续和小于0时,再加则一定会小于nums[i]的当前值,此时应该取nums[i]. #include #include using namespace std; int a[100001]; int sum[100001]; int main() { int n;

NK_test的博客 4025

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【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的博客 177

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

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

每周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. For example, given the array [2,3,

JacKnights的博客 318

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

Leetcode152. Maximum Product Subarray

找一个连续的子数组,使得连乘起来最大。

magic_jiayu的博客 204

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 p

Yano_nankai的博客 739

LeetCode //C - 152. Maximum Product Subarray

【代码】LeetCode //C - 152. Maximum Product Subarray。

Made in Code 835

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 larg...

取法于道的博客 307

152. Maximum Product Subarray【力扣】

题意理解 求一个数组的最大连续乘积的子序列 问题分析 数组 其他 参考 https://leetcode.com/problems/maximum-product-subarray/discuss/203013/C%2B%2B-O(N)-time-O(1)-space-solution-with-explanation 链接 int maxProduct(ve...

xiexie1357的专栏 301

【LeetCode】解题152:Maximum Product Subarray

LeetCode解题 152:Maximum Product SubarrayProblem 152: Maximum Product Subarray [Medium]解题思路Solution (Java) Problem 152: Maximum Product Subarray [Medium] Given an integer array nums, find the contiguous...

Fayedily的博客 260

leetcode 152. Maximum Product Subarray

Use prefix product(sum? actually much the same). class Solution { public int maxProduct(int[] nums) { int maxNeg = 1, minPos = 1; int product = 1; int ret = Integer.MIN_VAL...

weixin_30950237的博客 233

leetcode152 Maximum Product Subarray

求子数组的最大积. 我一开始联想到的是子数组的最大和,动态规划经典题目,但是这道题好像不能用DP来解. 我采用的是一种很直观的解法,求子数组最大积的时候,最特殊的就是0,0乘任何数都等于0.所以,假设有这样的一个数组 1,4,-5,9,-3,-4,0,1,-4,9,7,-3 0把这个数组分成了两个子数组,则最大积就是这两个子数组最大积的最大值(大多数情况下是,暂时不考虑特殊边界条件).所以...

LOVETEDA的博客 279
上一篇: [leetcode] 149. Max Points on a Line
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值