当前位置:网站首页>牛客网:乘积为正数的最长连续子数组
牛客网:乘积为正数的最长连续子数组
2022-06-30 15:44:00 【lsgoose】
1.非环形


注意审题!!
这里是求连续最长乘积为正数的长度是多少,我们维护两个长度,一个pos一个neg,pos代表到当前这个数为止的最长正数乘积,neg代表到当前这个数为止的最长负数乘积,我们对每个数进行如下操作:
1.若当前数为正,则最长的正数序列长度可以加一,若有最长负数序列,则也加一
2.若当前数为0,则所有以此数结尾的序列最长正数和负数序列都为0,故全部更新为0
3.若当前数为负,则需要交换pos和neg
具体代码如下所示:
#include<bits/stdc++.h>
using namespace std;
int dp(vector<int> &nums){
int res=0;
int pos=nums[0]>0 ? 1: 0;
int neg=nums[0]<0 ? 1: 0;
for(int i=1;i<nums.size();++i){
if(nums[i]>0){
pos=pos+1;
neg=neg>0 ? neg+1 : 0;
}else if(nums[i]==0){
pos=neg=0;
}else{
int tmp=neg>0 ? neg+1:0;
neg=pos+1;
pos=tmp;
}
res=max(res,pos);
}
return res;
}
int main(){
int n;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;++i){
cin>>nums[i];
}
cout<<dp(nums)<<endl;
return 0;
}2.环形数组


这里的思路参考了牛客里边的答案。
首先,我们都知道如何去求非循环数组的最大和,就是一遍线性dp,每次更新以当前数作为结尾的最大长度,那么如果是循环的呢?我们每次用一个新的起点来一次线性dp就可以求出。
具体代码如下所示:
#include<bits/stdc++.h>//超时的代码
using namespace std;
int solve(int n, vector<int> &v, vector<int> &dp);
int process2(int start, vector<int>&v);
int main(){
int n;
cin>>n;
vector<int> v(n);
for (int i = 0; i < n; ++i){
scanf("%d",&v[i]);
}
vector<int> dp(n, INT_MIN);
cout<<solve(n,v, dp)<<endl;
return 0;
}
int solve(int n, vector<int> &v, vector<int> &dp){
int ans = process2(0, v);
for(int i = 1; i < n; ++i){
ans = max(ans,process2(i,v));
}
return ans;
}
int process2(int start, vector<int>&v){
int ans = v[start];
for(int i = 1; i < v.size(); ++i){
ans = max(ans+v[(start + i)%v.size()],v[(start+i)%v.size()]);
}
return ans;
}
这样的时间复杂度是O(n^2),会超时。
- 切换看法,按照普通最大连续和的思路,即在dp的过程中可以得到最大的连续子数组和。
- 同理,如果再求一个最小的连续子数组和,就会发现一个特性。
- 假设用sum记录整个数组的和,那么最大连续子数组和dpmax 和最小连续子数组dpmin和一定是相连的两部分,因为假设不想连,中间这一段要么大于0,那么它应该属于最大子数组连续和,否则它应该属于最小连续子数组和。
- 故最后求得sum,dpmax,dpmin以后就可以进行判定。假设dpmax+dpmin<sum这说明数组两边的端点之和是正值,故应该把这一部分留给(dpmax=sum−dpmin),否则直接返回dpmax即可,当然这里有一个特例(在初始化dpmax的时候给予了数组的第一个值v[0],如果其是负数,则必定有dpmax+dpmin<sum 故此处应该特判,如果dpmax<0直接返回即可)
思路如下所示:
1.有环情况:先求出无环情况下连续子数组的最小值 res2,然后用数组和 sum 减去最小值 res2 即为环形情况下的连续子数组最大值
2.无环情况:最大子数组和
3.最大的环形子数组和 = max(最大子数组和,数组总和-最小子数组和),要排除全负数情况=最大子数组和
原文链接:https://blog.csdn.net/qq_35915636/article/details/123974337
#include<bits/stdc++.h>
using namespace std;
int dp(vector<int> &nums){
int sum, dpmx, dpmn, mx, mn;
dpmx=dpmn=mx=mn=sum=nums[0];
for(int i=1;i<nums.size();++i){
mx=max(mx+nums[i],nums[i]);
mn=min(mn+nums[i],nums[i]);
dpmx=max(dpmx, mx);
dpmn=min(dpmn, mn);
sum+=nums[i];
}
if(dpmx<0) return dpmx;
if(dpmx+dpmn<sum) dpmx=sum-dpmn;
return dpmx;
}
int main(){
int n;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;++i){
cin>>nums[i];
}
cout<<dp(nums)<<endl;
return 0;
}
边栏推荐
- Interview experience of service end test engineer
- What is XR extended reality and what are the XR cloud streaming platforms
- [CVE-2019-0193] - Apache Solr DataImport 远程命令执行分析
- Unsupported major.minor version 52.0
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
- microblaze 串口学习·2
- Interesting research on mouse pointer interaction
- “低代码”在企业数字化转型中扮演着什么角色?
- 云技能提升好伙伴,亚马逊云师兄今天正式营业
猜你喜欢

服务端测试工程师面试经验

【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例

The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution

Practical cases of data visualization (timeline rotation diagram, streamlit control year metabase visualization tutorial) 2.0

What role does "low code" play in enterprise digital transformation?
MySQL8.0开启远程连接权限的方法步骤

中国传奇教授李泽湘,正在批量制造独角兽

Go-Micro安装

Distributed machine learning: model average Ma and elastic average easgd (pyspark)
随机推荐
2022新消费半年盘点:行业遇冷,但这九个赛道依然吸金
Three development trends of enterprise application viewed from the third technological revolution
MC Instruction Decoder
Map reduce case super detailed explanation
Policy Center > Malware > Malware
MySQL master-slave configuration
ASP. Net core Middleware
Policy Center-Permissions and APIs that Access Sensitive Information
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
新茶饮“死去活来”,供应商却“盆满钵满”?
Google play index table
15年做糊21款硬件,谷歌到底栽在哪儿?
招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
猎头5万挖我去VC
Explain in detail the use of for loop, break and continue in go language
The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法
Mysql代理中间件Atlas安装和配置
How cloudxr promotes the future development of XR
halcon变量窗口的图像变量不显示,重启软件和电脑都没用