当前位置:网站首页>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;
}
}
边栏推荐
- CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
- 2022 P cylinder filling examination content and P cylinder filling practice examination video
- SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
- [Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification
- Recursive use and multi-dimensional array object to one-dimensional array object
- [brush questions] connected with rainwater (one dimension)
- Interaction free shell programming
- pytorch难学吗?如何学好pytorch?
- IPv6 foundation construction experiment
- eth入门之DAPP
猜你喜欢

Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)

2022 P cylinder filling examination content and P cylinder filling practice examination video

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

Recursion: quick sort, merge sort and heap sort

Bisher - based on SSM pet adoption center

Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练

Appium自动化测试框架

pytorch项目怎么跑?

nodejs基础:浅聊url和querystring模块

How to download pytorch? Where can I download pytorch?
随机推荐
MPLS setup experiment
有监督预训练!文本生成又一探索!
JS native common knowledge
Practical operation of vim
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
深潜Kotlin协程(十九):Flow 概述
Null and undefined
sigaction的使用
Deep dive kotlin synergy (20): build flow
阿洛对自己的思考
Arduino application development - LCD display GIF dynamic diagram
【毕业季·进击的技术er】职场人的自白
Cnopendata China Customs Statistics
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
What is pytorch? Is pytorch a software?
Commands related to the startup of redis under Linux server (installation and configuration)
Supervised pre training! Another exploration of text generation!
[brush questions] connected with rainwater (one dimension)
Filter