当前位置:网站首页>LeetCode 0152. Product Maximum Subarray: dp + Roll in Place
LeetCode 0152. Product Maximum Subarray: dp + Roll in Place
2022-08-01 18:20:00 【Tisfy】
【LetMeFly】152.乘积最大子数组:dp + 原地滚动
力扣题目链接:https://leetcode.cn/problems/maximum-product-subarray/
给你一个整数数组 nums
,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积.
测试用例的答案是一个 32-位 整数.
子数组 是数组的连续子序列.
示例 1:
输入: nums = [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6.
示例 2:
输入: nums = [-2,0,-1] 输出: 0 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组.
提示:
1 <= nums.length <= 2 * 104
-10 <= nums[i] <= 10
nums
的任何前缀或后缀的乘积都 保证 是一个 32-位 整数
方法一:dp + 原地滚动
需要两个变量 m m m和 M M M,Respectively, it ends with the currently processed number乘积最大子数组
初始值 m m m和 M M Mare the first elements in the arraynums[0]
i i i从下标1
开始遍历数组,Since you want to subscript i i iis the end of a contiguous array,Then there are three options:
- Select only the current subscript as i i i的元素( n u m s [ i ] nums[i] nums[i])
- Use the largest product of subarrays ending with the previous element 乘上 这个元素( n u m s [ i ] ∗ M nums[i] * M nums[i]∗M)
- Use the least product of subarrays ending with the previous element 乘上 这个元素( n u m s [ i ] ∗ m nums[i] * m nums[i]∗m)
Each time it traverses to each element,Calculate the three new possible extrema above,并更新 m m m和 M M M,At the same time, record the maximum value of the answer during the entire traversal process.
Q&S: Why even record the minimum value m m minstead of just recording the maximum value M M M?
Because the maximum value may be obtained by multiplying two negative numbers.If you multiply two negative numbers,The smaller the negative number, the larger the product.
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是数组
nums
中元素的个数 - 空间复杂度 O ( 1 ) O(1) O(1)
AC代码
C++
class Solution {
public:
int maxProduct(vector<int>& nums) {
int ans = nums[0];
int m = nums[0], M = nums[0];
for (int i = 1; i < nums.size(); i++) {
int timesLastm = m * nums[i];
int timesLastM = M * nums[i];
m = min(nums[i], min(timesLastm, timesLastM));
M = max(nums[i], max(timesLastm, timesLastM));
ans = max(ans, M);
}
return ans;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126094071
边栏推荐
- QT basic functions, signals, slots
- How to make the fixed-point monitoring equipment display the geographic location on the EasyCVR platform GIS electronic map?
- 请你说说多线程
- 2022,程序员应该如何找工作
- opencv syntax Mat type summary
- 解决MySQL插入不了中文数据问题
- 【Day_12 0507】查找组成一个偶数最接近的两个素数
- Leetcode75. Color Classification
- 国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
- 将ENS域名转化为音乐需要几步?
猜你喜欢
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
ExcelPatternTool: Excel表格-数据库互导工具
Topology Parts Disassembly 3D Visualization Solution
8月微软技术课程,欢迎参与
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
Leetcode74. 搜索二维矩阵
电商库存系统的防超卖和高并发扣减方案
Leetcode71. Simplified Paths
opencv syntax Mat type summary
el-form-item prop属性动态绑定不生效如何解决
随机推荐
史上最全的Redis基础+进阶项目实战总结笔记
【Day_11 0506】 最近公共祖先
typora操作手册
突破性能天花板!亚信数据库支撑 10 多亿用户,峰值每秒百万交易
COS 用户实践征文
QPalette palette, frame color fill
QLineEdit learning and use
tooltip control
Leetcode75. 颜色分类
2022年 PHP面试问题记录
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
理财产品的月年化收益率怎么算?
【Day_12 0507】查找组成一个偶数最接近的两个素数
Three solutions: npm WARN config global --global, --local are deprecated. Use --location=global instead.
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
【Day_12 0507】二进制插入
AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
483-82(23、239、450、113)
阿里云的域名和ip绑定