当前位置:网站首页>模拟卷Leetcode【普通】1567. 乘积为正数的最长子数组长度
模拟卷Leetcode【普通】1567. 乘积为正数的最长子数组长度
2022-07-07 06:18:00 【邂逅模拟卷】
1567. 乘积为正数的最长子数组长度
给你一个整数数组 nums ,请你求出乘积为正数的最长子数组的长度。
一个数组的子数组是由原数组中零个或者更多个连续数字组成的数组。
请你返回乘积为正数的最长子数组长度。
示例 1:
输入:nums = [1,-2,-3,4]
输出:4
解释:数组本身乘积就是正数,值为 24 。
示例 2:
输入:nums = [0,1,-2,-3,-4]
输出:3
解释:最长乘积为正数的子数组为 [1,-2,-3] ,乘积为 6 。
注意,我们不能把 0 也包括到子数组中,因为这样乘积为 0 ,不是正数。
示例 3:
输入:nums = [-1,-2,-3,0,1]
输出:2
解释:乘积为正数的最长子数组是 [-1,-2] 或者 [-2,-3] 。
示例 4:
输入:nums = [-1,2]
输出:1
示例 5:
输入:nums = [1,2,3,5,-6,4,0,10]
输出:4
提示:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-length-of-subarray-with-positive-product
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
""" 动态规划,保存正负最长的长度,对于每个数分>0,=0,<0三种情况: - >0: 正的长度+1,负的长度得看之前负的最长是不是0 - =0: 正负的长度都为0 - <0: 正的长度看之前负的,负的长度=之前正的+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_乘积最大子数组(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')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- 测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- Data analysis methodology and previous experience summary 2 [notes dry goods]
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
- 数据库存储---表分区
- Componentspace2022, assertions, protocols, bindings, and configuration files
- Gson converts the entity class to JSON times declare multiple JSON fields named
- Image segmentation in opencv
- Uniapp wechat applet monitoring network
- Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
猜你喜欢
【踩坑】nacos注册一直连接localhost:8848,no available server
Interpolation lookup (two methods)
详解华为应用市场2022年逐步减少32位包体上架应用和策略
[MySQL] detailed explanation of trigger content of database advanced
Compilation and linking of programs
快速集成认证服务-HarmonyOS平台
Greenplum 6.x reinitialization
Greenplum6.x常用语句
About using CDN based on Kangle and EP panel
数据分片介绍
随机推荐
[Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
leetcode135. Distribute candy
Other 7 features of TCP [sliding window mechanism ▲]
NCS Chengdu Xindian interview experience
Leetcode 1984. Minimum difference in student scores
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
年薪50w阿里P8亲自下场,教你如何从测试进阶
Greenplum6.x监控软件搭建
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
Required String parameter ‘XXX‘ is not present
PPT模板、素材下载网站(纯干货,建议收藏)
Mock. JS usage details
Greenplum 6.x reinitialization
联想混合云Lenovo xCloud:4大产品线+IT服务门户
数据分片介绍
详解华为应用市场2022年逐步减少32位包体上架应用和策略
Quick sorting (detailed illustration of single way, double way, three way)
You should use Google related products with caution
Analysis of abnormal channel number information before and after AGC re signature service
Markdown editor Use of MD plug-in