当前位置:网站首页>The longest subarray length with a positive product of 1567 recorded by leecode
The longest subarray length with a positive product of 1567 recorded by leecode
2022-07-03 04:01:00 【Why is there a bug list】
topic
Give you an array of integers nums , Please find the length of the longest subarray whose product is positive .
The subarray of an array is an array of zero or more consecutive numbers in the original array .
Please return the length of the longest subarray whose product is a positive number .
Example 1:
Input :nums = [1,-2,-3,4]
Output :4
explain : The product of the array itself is a positive number , The value is 24 .
Example 2:
Input :nums = [0,1,-2,-3,-4]
Output :3
explain : The subarray with the longest positive product is [1,-2,-3] , The product is 6 .
Be careful , We can't 0 Also included in the subarray , Because the product is 0 , Not positive. .
Example 3:
Input :nums = [-1,-2,-3,0,1]
Output :2
explain : The longest subarray whose product is a positive number is [-1,-2] perhaps [-2,-3] .
Tips :
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
answer
class Solution {
public int getMaxLen(int[] nums) {
List<List<Integer>> lists = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
lists.add(temp);
for(int i = 0;i < nums.length;i++){
if(nums[i] != 0){
temp.add(nums[i]);
}else{
if(temp.size() != 0){
lists.add(copy(temp));
temp = new ArrayList<>();
lists.add(temp);
}
}
}
int res = 0;
for(List<Integer> list : lists){
int min = -1;
int max = -1;
int count = 0;
boolean isFirst = true;
int i = 0;
while(i < list.size()){
if(list.get(i) < 0){
count++;
if(isFirst){
min = i;
isFirst = false;
max = i;
}else{
max = i;
}
}
i++;
}
if(count % 2 != 0){
int ex = Math.min(min + 1,list.size() - max);
res = Math.max(res,list.size() - ex);
}else{
res = Math.max(res,list.size());
}
}
return res;
}
private List<Integer> copy(List<Integer> src){
List<Integer> copy = new ArrayList<>();
for(int i : src){
copy.add(i);
}
return copy;
}
}
边栏推荐
- js中#号的作用
- When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker
- Applet (continuous update)
- CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
- [Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
- Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积
- C language hashtable/hashset library summary
- How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
- Commands related to the startup of redis under Linux server (installation and configuration)
- Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
猜你喜欢

Mila, University of Ottawa | molecular geometry pre training with Se (3) invariant denoising distance matching

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强

CVPR 2022 | Dalian Technology propose un cadre d'éclairage auto - étalonné pour l'amélioration de l'image de faible luminosité de la scène réelle

用户体验五要素

Bisher - based on SSM pet adoption center

Is pytorch difficult to learn? How to learn pytorch well?

China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects

js实现在可视区内,文字图片动画效果

Web会话管理安全问题

学会pytorch能干什么?
随机推荐
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数
Ffmpeg download and installation tutorial and introduction
China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
Debug: CD cannot be used in kaggle
Write it down once Net travel management background CPU Explosion Analysis
没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
QSAR model establishment script based on pytoch and rdkit
Social phobia of contemporary young people (II)
[mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
深潜Kotlin协程(十九):Flow 概述
递归:一维链表和数组
Recursion: quick sort, merge sort and heap sort
NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
[brush questions] connected with rainwater (one dimension)
Interaction free shell programming
Nodejs Foundation: shallow chat URL and querystring module
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强