当前位置:网站首页>Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
2022-07-07 08:55:00 【Encounter simulation volume】
1567. The longest subarray length whose product is a positive number
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] .
Example 4:
Input :nums = [-1,2]
Output :1
Example 5:
Input :nums = [1,2,3,5,-6,4,0,10]
Output :4
Tips :
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/maximum-length-of-subarray-with-positive-product
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def __init__(self):
""" Dynamic programming , Save the longest length of positive and negative , For each score >0,=0,<0 Three situations : - >0: Positive length +1, The negative length depends on whether the previous negative is the longest 0 - =0: Both positive and negative lengths are 0 - <0: The positive length looks at the previous negative , Negative length = It was +1 """
pass
def getMaxLen(self, nums: List[int]) -> int:
len_p, len_n = int(nums[0] > 0), int(nums[0] < 0)
res = len_p
for num in nums[1::]:
if num > 0:
len_p, len_n = len_p + 1, (len_n + 1 if len_n > 0 else 0)
elif num < 0:
len_p, len_n = (len_n + 1 if len_n > 0 else 0), len_p + 1
else:
len_p, len_n = 0, 0
res = max(res, len_p)
return res
def maxProduct_152_ Product maximum subarray (self, nums: List[int]) -> int:
max_save,min_save,max_all =nums[0],nums[0],nums[0]
for i,num in enumerate(nums):
if i==0:continue
max_save,min_save = max(max(max_save*num,num),min_save*num),min(min(max_save*num,num),min_save*num)
return max(max_all,max_save)
def test(data_test):
s = Solution()
return s.getMaxLen(*data_test)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[[1,-2,-3,4]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
猜你喜欢

About using CDN based on Kangle and EP panel

Synchronized underlying principle, volatile keyword analysis

How to integrate app linking services in harmonyos applications

oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量

Three series of BOM elements

Markdown editor Use of MD plug-in

idea里使用module项目的一个bug
![Other 7 features of TCP [sliding window mechanism ▲]](/img/ff/c3f52a7b89804acfd0c4f3b78bc4a0.jpg)
Other 7 features of TCP [sliding window mechanism ▲]

IP地址的类别

let const
随机推荐
How to add a mask of a target in a picture
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
Greenplum6.x常用语句
Count sort (diagram)
硬件大熊原创合集(2022/05更新)
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
leetcode134. gas station
JS的操作
Goldbach conjecture C language
Rapid integration of authentication services - harmonyos platform
阿里p8手把手教你,自动化测试应该如何实现多线程?赶紧码住
Isomorphic C language
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider
LeetCode 715. Range 模块
Category of IP address
模拟卷Leetcode【普通】1567. 乘积为正数的最长子数组长度
Frequently Asked Coding Problems
Platformization, a fulcrum of strong chain complementing chain