当前位置:网站首页>Leetcode-152- product maximum subarray
Leetcode-152- product maximum subarray
2022-07-27 22:12:00 【benym】
# LeetCode-152- Product maximum subarray
Give you an array of integers nums , Please find the continuous subarray with the largest product in the array ( The subarray contains at least one number ), And returns the product of the subarray .
Example 1:
Input : [2,3,-2,4]
Output : 6
explain : Subarray [2,3] There is a maximum product 6.Example 2:
Input : [-2,0,-1]
Output : 0
explain : The result can't be 2, because [-2,-1] It's not a subarray .# Their thinking
Method 1、 Dynamic programming :
When traversing the array, constantly calculate the current maximum value
At the same time, it is also necessary to record the previous minimum value , When traversing to nums[i] When it's negative
Maximum * negative : Will cause the maximum value to become the minimum
minimum value * negative : It will cause the minimum value to become the maximum
Therefore, it is necessary to maintain the current maximum value and the current minimum value
The maximum value can be determined by curMax = Math.max(curMax*nums[i],nums[i]); Calculation
The minimum value can be determined by curMin = Math.min(curMin*nums[i],nums[i]); Calculation
The maximum and minimum values are interchangeable , The result is wrong
In that case, when you encounter negative numbers nums[i] When , Swap the maximum and minimum values in advance , The original maximum and minimum values can be maintained
A better solution comes from https://leetcode-cn.com/problems/maximum-product-subarray/solution/dpfang-fa-xiang-jie-by-yang-cong-12/
# Java Code
class Solution {
public int maxProduct(int[] nums) {
int max = Integer.MIN_VALUE;
int curMax = 1;
int curMin = 1;
for(int i=0;i<nums.length;i++){
if(nums[i]<0){
int temp = curMax;
curMax = curMin;
curMin = temp;
}
curMax = Math.max(curMax*nums[i],nums[i]);
curMin = Math.min(curMin*nums[i],nums[i]);
max = Math.max(max,curMax);
}
return max;
}
}dp Array version
# Java Code
class Solution {
public int maxProduct(int[] nums) {
int[] dp_max = new int[nums.length+1];
int[] dp_min = new int[nums.length+1];
if(nums.length == 0) return 0;
int max = Integer.MIN_VALUE;
// Because there are negative numbers , So you need to maintain two arrays
// dp_max[i] Refers to the first i Number ending Product maximum A continuous subsequence of
// dp_min[i] Refers to the first i Number ending The product is the smallest A continuous subsequence of
dp_max[0] = 1;
dp_min[0] = 1;
for (int i = 1;i <= nums.length;i++){
// If the number of arrays is negative , It will lead to max become min,min become max
// So we need to exchange dp
if(nums[i-1] < 0){
int temp = dp_min[i-1];
dp_min[i-1] = dp_max[i-1];
dp_max[i-1] = temp;
}
dp_min[i] = Math.min(nums[i-1],dp_min[i-1]*nums[i-1]);
dp_max[i] = Math.max(nums[i-1],dp_max[i-1]*nums[i-1]);
max = Math.max(max,dp_max[i]);
}
return max;
}
}边栏推荐
- Is it safe to open an account online now? Then choose which securities to open a securities account
- 刚培训完的中级测试工程师如何快速度过试用期
- 一口气学完 Redis 集群方案
- For 3nm and below processes, ASML new generation EUV lithography machine exposure
- Pythia: Facebook's latest open source visual and language multitasking learning framework
- [stonedb fault diagnosis] MDL lock waiting
- Station B collapsed. If we were the developer responsible for the repair that night
- V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
- Array expansion, sorting, nested statement application
- 高频继电器
猜你喜欢

Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor

Log4j 漏洞仍普遍存在?

Matlab 绘制风速、风向统计玫瑰花图

JVM memory model interview summary

7行代码让B站崩溃3小时

B站崩了,那晚负责修复的开发人员做了什么?

Microsoft store can't download apps, vs2019 can't download plug-ins solution

Project analysis (what is it training that can't be given)

B站崩了,如果我们是那晚负责修复的开发人员

Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
随机推荐
Apachespark command execution (cve-2022-33891) vulnerability recurrence
Nano semiconductor 65W gallium nitride (GAN) scheme was adopted by Xiaomi 10 Pro charger
Seven lines of code crashed station B for three hours
Drawing three coordinate (axis) diagram with MATLAB
day 1 - day 4
Software testing interview question: what is the focus of unit testing, integration testing, and system testing?
8000 word explanation of OBSA principle and application practice
8000字讲透OBSA原理与应用实践
数组扩容、排序、嵌套语句应用
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?
2021-11-05 understanding of class variables and class methods
More than 100 lines should be split into functions
Common shortcut keys and setting methods of idea
Station B collapsed. What did the developer responsible for the repair do that night?
Can JVM tuning be done with single core CPU and 1G memory?
Learn the use principle and core idea of thread pool from the source code
Mask automatic update description file (mask description file)
QT take out the input box string, lineedit
Basic usage of two-dimensional array
IDEA常用快捷键及设置方法