当前位置:网站首页>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;
}
}
边栏推荐
- 2022年已过半,得抓紧
- 阿洛对自己的思考
- 【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
- Deep dive kotlin synergy (20): build flow
- IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
- Role of JS No
- 中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》
- Download and install captura and configure ffmpeg in captura
- [home push IMessage] software installation virtual host rental tothebuddy delay
- SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
猜你喜欢

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

What can learning pytorch do?

如何迈向IPv6之IPv6过渡技术-尚文网络奎哥

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

How does the pytorch project run?

2022 mobile crane driver examination registration and mobile crane driver operation examination question bank

【刷题篇】 找出第 K 小的数对距离

Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis

Role of JS No

Appium automated testing framework
随机推荐
动态规划:最长公共子串和最长公共子序列
Application of I2C protocol of STM32F103 (read and write EEPROM)
Recursive use and multi-dimensional array object to one-dimensional array object
MPLS setup experiment
Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
Introduction to eth
Interaction free shell programming
eth入门之简介
Dynamic programming: Longest palindrome substring and subsequence
树莓派如何连接WiFi
阿洛对自己的思考
记一次 .NET 差旅管理后台 CPU 爆高分析
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
js中#号的作用
Arlo's thinking about himself
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
【刷题篇】 找出第 K 小的数对距离
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
JS实现图片懒加载