当前位置:网站首页>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;
}
}
边栏推荐
- SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
- [learning notes] seckill - seckill project - (11) project summary
- Applet (continuous update)
- Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
- [brush questions] connected with rainwater (one dimension)
- 树莓派如何连接WiFi
- pytorch是什么?pytorch是一个软件吗?
- 【刷题篇】多数元素(超级水王问题)
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
- 因果AI,下一代可信AI的产业升级新范式?
猜你喜欢
pytorch难学吗?如何学好pytorch?
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
[home push IMessage] software installation virtual host rental tothebuddy delay
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
2022 P cylinder filling examination content and P cylinder filling practice examination video
300+篇文献!一文详解基于Transformer的多模态学习最新进展
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
Recursion: quick sort, merge sort and heap sort
随机推荐
【刷题篇】 找出第 K 小的数对距离
Intercept string fixed length to array
记一次 .NET 差旅管理后台 CPU 爆高分析
深潜Kotlin协程(十九):Flow 概述
eth入门之简介
leetcode:297. 二叉树的序列化与反序列化
Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
Social phobia of contemporary young people (III)
Separable bonds and convertible bonds
C language hashtable/hashset library summary
Bisher - based on SSM pet adoption center
8.8.2-PointersOnC-20220214
pytorch是什么?pytorch是一个软件吗?
[Yu Yue education] reference materials of political communication science of Communication University of China
Makefile demo
nodejs基础:浅聊url和querystring模块
[brush questions] find the number pair distance with the smallest K
2022 polymerization process examination questions and polymerization process examination skills
Shardingsphere dynamic data source
"Designer universe" argument: Data Optimization in the design field is finally reflected in cost, safety and health | chinabrand.com org