当前位置:网站首页>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] detailed explanation of trigger content of database advanced
- Quick sorting (detailed illustration of single way, double way, three way)
- Three series of BOM elements
- opencv 将16位图像数据转为8位、8转16
- 对API接口或H5接口做签名认证
- Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and
- Count sort (diagram)
- Mock. JS usage details
- 注解@ConfigurationProperties的三种使用场景
- MySQL partition explanation and operation statement
猜你喜欢
随机推荐
MySQL partition explanation and operation statement
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
Opencv converts 16 bit image data to 8 bits and 8 to 16
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider
Greenplum6.x搭建_环境配置
Required String parameter ‘XXX‘ is not present
let const
如何在图片的目标中添加目标的mask
FPGA knowledge accumulation [6]
Redis fault handling "can't save in background: fork: cannot allocate memory“
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
Quick sorting (detailed illustration of single way, double way, three way)
Other 7 features of TCP [sliding window mechanism ▲]
Frequently Asked Coding Problems
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
Image segmentation in opencv
go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳