当前位置:网站首页>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
边栏推荐
- 【翻译】CNCF培养的OpenMetrics成为一个孵化项目
- 【Day_12 0507】二进制插入
- 直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
- 7月30号|来一场手把手助您打造智能视觉新爆款的技术动手实验
- Live chat system technology (8) : vivo live IM message module architecture practice in the system
- Leetcode73. 矩阵置零
- Basic image processing in opencv
- QT基础功能,信号、槽
- QLineEdit learning and use
- 中信证券是国内十大券商吗?怎么开户安全?
猜你喜欢
随机推荐
SQL的substring_index()用法——MySQL字符串截取
QT_QThread thread
SQL的ROUND函数用法及其实例
理财产品的月年化收益率怎么算?
Leetcode71. 简化路径
Live chat system technology (8) : vivo live IM message module architecture practice in the system
C language theory--a solid foundation for the written test and interview
WinRAR | 将多个安装程序生成一个安装程序
打开微信客服
OpenCV installation, QT, VS configuration project settings
QT常用全局宏定义
opencv real-time face detection
sql添加索引
XAML WPF项目groupBox控件
云原生全景图详解
Leetcode75. Color Classification
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
JVM运行时数据区与JMM内存模型是什么
三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.
暑假第二周总结博客