当前位置:网站首页>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] <= 10nums的任何前缀或后缀的乘积都 保证 是一个 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
边栏推荐
猜你喜欢

B005 - STC8 based single chip microcomputer intelligent street light control system

打开微信客服

How to use the Golang coroutine scheduler scheduler

XAML WPF项目groupBox控件

Three solutions: npm WARN config global --global, --local are deprecated. Use --location=global instead.

【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)

How many steps does it take to convert an ENS domain name into music?

Solve the problem that MySQL cannot insert Chinese data

MySQL 45 Talk | 09 How to choose common index and unique index?

云原生全景图详解
随机推荐
【Day_11 0506】求最大连续bit数
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
The function realization of the national standard GB28181 protocol EasyGBS platform compatible with the old version of the inflow port
Topology Parts Disassembly 3D Visualization Solution
LeetCode 1374.生成每种字符都是奇数个的字符串
B001 - 基于STM32的智能生态鱼缸
How to solve the dynamic binding of el-form-item prop attribute does not take effect
成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
计算IoU(D2L)
打开微信客服
史上最全的Redis基础+进阶项目实战总结笔记
QLineEdit学习与使用
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
opencv syntax Mat type summary
C语言理论--笔试面试基础稳固
QT_QDialog 对话框
【Day_11 0506】 最近公共祖先
Tower Defense Shoreline User Agreement