当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Mountaineering team (DFS)
- 求有符号数的原码、反码和补码【C语言】
- 年薪50w阿里P8亲自下场,教你如何从测试进阶
- Componentspace2022, assertions, protocols, bindings, and configuration files
- opencv 将16位图像数据转为8位、8转16
- Implement custom memory allocator
- [Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
- 更改当前文件夹及文件夹下文件日期shell脚本
- Markdown editor Use of MD plug-in
- [MySQL] detailed explanation of trigger content of database advanced
猜你喜欢

Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up

Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical

NCS Chengdu New Electric interview Experience
![[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources](/img/e3/3703bdace2d0ca47c1a585562dc15e.jpg)
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources

MySQL主从延迟的解决方案

Digital triangle model acwing 1027 Grid access

指针进阶,字符串函数
![[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology](/img/2a/01db1b84c26502c851786aaca56abe.jpg)
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology

数字三角形模型 AcWing 275. 传纸条
![FPGA knowledge accumulation [6]](/img/db/c3721c3e842ddf4c1088a3f54e9f2a.jpg)
FPGA knowledge accumulation [6]
随机推荐
leetcode135. Distribute candy
Mock.js用法详解
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
Greenplum6.x监控软件搭建
Speaking of a software entrepreneurship project, is there anyone willing to invest?
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
Three series of BOM elements
数据分片介绍
对API接口或H5接口做签名认证
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
Greenplum 6.x monitoring software setup
You should use Google related products with caution
Mock. JS usage details
Three usage scenarios of annotation @configurationproperties
Greenplum6.x搭建_安装
Count sort (diagram)
let const
Arm GIC (IV) GIC V3 register class analysis notes.
LeetCode 736. Lisp 语法解析
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