当前位置:网站首页>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 !
边栏推荐
- 【MySQL】数据库进阶之触发器内容详解
- 年薪50w阿里P8亲自下场,教你如何从测试进阶
- Greenplum6.x-版本变化记录-常用手册
- With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
- Redis summary
- Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
- ChaosBlade:混沌工程简介(一)
- RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
- MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
- JS的操作
猜你喜欢
LeetCode 715. Range module
Platformization, a fulcrum of strong chain complementing chain
Simple use of Xray
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Greenplum6.x常用语句
leetcode135. Distribute candy
Synchronized underlying principle, volatile keyword analysis
Goldbach conjecture C language
【MySQL】数据库进阶之触发器内容详解
数字三角形模型 AcWing 275. 传纸条
随机推荐
OpenGL三维图形绘制
测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
【istio简介、架构、组件】
let const
Calculation s=1+12+123+1234+12345 C language
LeetCode 715. Range module
Simple use of Xray
With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
【ChaosBlade:根据标签删除POD、Pod 域名访问异常场景、Pod 文件系统 I/O 故障场景】
Three usage scenarios of annotation @configurationproperties
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
Redis summary
systemd
[Nanjing University] - [software analysis] course learning notes (I) -introduction
【微信小程序:缓存操作】
go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
数字三角形模型 AcWing 275. 传纸条
Greenplum 6.x version change record common manual
Image segmentation in opencv
Greenplum6.x常用语句