当前位置:网站首页>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;
}
}边栏推荐
- A lock faster than read-write lock. Don't get to know it quickly
- Interview question: talk about your understanding of AQS
- @Component可以和@Bean 用在同一个类上吗?
- Live broadcast software app development, uniapp scroll view hidden scroll bar
- [question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
- 异常-Exception
- Can JVM tuning be done with single core CPU and 1G memory?
- It seems to be a bug of thread pool, but I think the source code design is unreasonable.
- Lvs+kept highly available cluster
- 8000 word explanation of OBSA principle and application practice
猜你喜欢

Small change project (two versions) with detailed ideas

The design idea of relational database is obvious to you in 20 pictures

Talk about MySQL transaction two-phase commit

Seven lines of code crashed station B for three hours

项目分析(哪些是it培训给不了)

The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"

Reentranlock and source code analysis (learn ideas and click the source code step by step)

Log4j 漏洞仍普遍存在?

Log4j vulnerability is still widespread and continues to cause impact

2022 2nd cyber edge cup cyber security competition Web
随机推荐
成员方法及其传参机制
Apachespark command execution (cve-2022-33891) vulnerability recurrence
学完4种 Redis 集群方案要多久?我一口气给你说完
It seems to be a bug of thread pool, but I think the source code design is unreasonable.
[question 23] Sudoku game with rotation | DFS (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
刚培训完的中级测试工程师如何快速度过试用期
An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
[Marine Science] climate indices data set
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?
Software testing interview question: when does the software testing project start? Why?
2021-11-05 understanding of class variables and class methods
Basic usage of two-dimensional array
Software test interview question: suppose there is a text box that requires the input of a 10 character postal code, how should the text box be divided into equivalent classes?
Internal class (detailed explanation of four internal classes)
Is log4j vulnerability still widespread?
Software testing interview question: what is the focus of unit testing, integration testing, and system testing?
Implementation of arbitrary code execution based on.Net dynamic compilation technology
V2.x synchronization is abnormal. There are a lot of posts that cannot be synchronized in the cloud, and the synchronization is blocked and slow
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?
Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor