当前位置:网站首页>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;
}
}
边栏推荐
- Supervised pre training! Another exploration of text generation!
- Applet (continuous update)
- Introduction to eth
- 用户体验五要素
- In Net 6 project using startup cs
- pytorch怎么下载?pytorch在哪里下载?
- pytorch难学吗?如何学好pytorch?
- Interaction free shell programming
- 【刷题篇】 找出第 K 小的数对距离
- [mathematical logic] predicate logic (predicate logic basic equivalent | eliminate quantifier equivalent | quantifier negative equivalent | quantifier scope contraction expansion equivalent | quantifi
猜你喜欢
Makefile demo
How does the pytorch project run?
Download and install captura and configure ffmpeg in captura
[brush questions] connected with rainwater (one dimension)
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
Makefile demo
"Final review" 16/32-bit microprocessor (8086) basic register
用户体验五要素
nodejs基础:浅聊url和querystring模块
Ffmpeg download and installation tutorial and introduction
随机推荐
What can learning pytorch do?
2022 P cylinder filling examination content and P cylinder filling practice examination video
Dynamic programming: longest common substring and longest common subsequence
树莓派如何连接WiFi
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
第十届中国云计算大会·中国站:展望未来十年科技走向
Deep dive kotlin synergy (19): flow overview
The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units
【DRM】DRM bridge驱动调用流程简单分析
Debug: CD cannot be used in kaggle
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
[learning notes] seckill - seckill project - (11) project summary
Supervised pre training! Another exploration of text generation!
Web会话管理安全问题
pytorch项目怎么跑?
eth入门之DAPP
Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
Bisher - based on SSM pet adoption center
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis