当前位置:网站首页>Leetcode notes - dynamic planning -day6
Leetcode notes - dynamic planning -day6
2022-07-06 15:16:00 【LL. LEBRON】
List of articles
LeetCode Brush notes - Dynamic programming -day6
152. Product maximum subarray
1. subject
Original link :152. Product maximum subarray
2. Their thinking
Algorithm : Dynamic programming + Rolling array optimization
- f[i] All from 0 To i And choose a[i] The maximum product obtained
- g[i] All from 0 To i And choose a[i] Minimum product obtained
There are several situations :
- When a[i] >= 0 when ,f[i] = max(a[i], f[i - 1] * a[i])
- When a[i] < 0 when ,f[i] = max(a[i], g[i - 1] * a[i])
- When a[i] >= 0 when ,g[i] = min(a[i], g[i - 1] * a[i])
- When a[i] < 0 when ,g[i] = min(a[i], f[i - 1] * a[i])
Can be combined into :
- When
a[i] >= 0
whenf[i] = max(a[i], max(f[i-1] * a[i],g[i-1]*a[i]))
- When
a[i]<0
whenf[i] = max(a[i], max(g[i-1] * a[i],f[i-1]*a[i])
You can use scrolling arrays to optimize space . See the code for details .
3. Code
class Solution {
public:
int maxProduct(vector<int>& a) {
int f=a[0],g=a[0];
int res=a[0];
for(int i=1;i<a.size();i++){
int t=a[i],fa=f*t,ga=g*t;
f=max(t,max(fa,ga));
g=min(t,min(fa,ga));
res=max(res,f);
}
return res;
}
};
1567. The longest subarray length whose product is a positive number
1. subject
Original link :1567. The longest subarray length whose product is a positive number
2. Their thinking
Algorithm : Dynamic programming
We can use two arrays f[i]
and g[i]
:
f[i]
Denotes the following i The length of the longest subarray with a positive product at the endg[i]
Denotes the following i The length of the longest subarray with a negative product at the end
Here we can get the recurrence formula :
- If the current number is greater than 0, namely
nums[i]>0
, Multiply the previous product by the current number , The positive and negative will not change , therefore :f[i]=f[i-1]+1
- If
g[i-1]
It's not equal to 0 Words , One more , If g[i-1] As such 0, A positive number here will not change
- If the current number is less than 0, namely
nums[i]<0
, Multiplying the previous product by the current number will change the positive and negative of the product , therefore :- Now
g[i]
It should be equal tof[i-1]+1
, becausef[i-1]
The product of contained numbers is a positive number , Multiplying by the current number is just a negative number . f[i]
You need to considerg[i-1]
situation , Ifg[i-1]
by 0, heref[i]
Also for 0, otherwisef[i]=g[i-1]+1
, A negative number multiplied by a negative number is a positive number
- Now
- If the current number is 0, namely
nums[i]==0
, takef[i]
,g[i]
The assignment is 0 - Maintain the maximum value for each iteration :
res=max(res,f[i]);
3. Code
class Solution {
public:
int getMaxLen(vector<int>& nums) {
int f=0,g=0;
if(nums[0]>0) f=1;
else if(nums[0]<0) g=1;
int res=f;
for(int i=1;i<nums.size();i++){
if(nums[i]>0){
f++;
g=(g==0)?0:g+1;
}else if(nums[i]<0){
int t=f;
f=(g==0)?0:g+1;
g=t+1;
}else{
f=0,g=0;
}
res=max(res,f);
}
return res;
}
};
边栏推荐
- What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
- HackTheBox-Emdee five for life
- Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
- Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
- 安全测试入门介绍
- Currently, mysql5.6 is used. Which version would you like to upgrade to?
- In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
- Want to learn how to get started and learn software testing? I'll give you a good chat today
- Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
猜你喜欢
Eigen User Guide (Introduction)
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
自动化测试你必须要弄懂的问题,精品总结
What are the software testing methods? Show you something different
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
Fundamentals of digital circuits (III) encoder and decoder
Cadence physical library lef file syntax learning [continuous update]
Automated testing problems you must understand, boutique summary
Servlet
How to rename multiple folders and add unified new content to folder names
随机推荐
Eigen User Guide (Introduction)
Which version of MySQL does php7 work best with?
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
How to use Moment. JS to check whether the current time is between 2 times
Detailed introduction to dynamic programming (with examples)
What is "test paper test" in software testing requirements analysis
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
Description of Vos storage space, bandwidth occupation and PPS requirements
软件测试行业的未来趋势及规划
UCORE lab7 synchronous mutual exclusion experiment report
Sleep quality today 81 points
Global and Chinese markets for complex programmable logic devices 2022-2028: Research Report on technology, participants, trends, market size and share
软件测试需求分析之什么是“试纸测试”
CSAPP Shell Lab 实验报告
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Cadence physical library lef file syntax learning [continuous update]
软件测试工作太忙没时间学习怎么办?
Public key box
UCORE lab2 physical memory management experiment report
Do you know the performance testing terms to be asked in the software testing interview?