当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- 平台化,强链补链的一个支点
- Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
- Componentspace2022, assertions, protocols, bindings, and configuration files
- LeetCode 715. Range module
- 【踩坑】nacos注册一直连接localhost:8848,no available server
- Enterprise manager cannot connect to the database instance
- 指针进阶,字符串函数
- Three usage scenarios of annotation @configurationproperties
- 如何在快应用中实现滑动操作组件
- 更改当前文件夹及文件夹下文件日期shell脚本
猜你喜欢

Count sort (diagram)

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

Rapid integration of authentication services - harmonyos platform

Greenplum 6.x common statements

Download and install orcale database11.2.0.4

Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022

Simple use of Xray

The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider

Nanjing commercial housing sales enabled electronic contracts, and Junzi sign assisted in the online signing and filing of housing transactions

Digital triangle model acwing 275 Pass a note
随机推荐
Problems encountered in the use of go micro
NCS Chengdu New Electric interview Experience
数字三角形模型 AcWing 275. 传纸条
模拟卷Leetcode【普通】1706. 球会落何处
Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical
opencv之图像分割
Speaking of a software entrepreneurship project, is there anyone willing to invest?
实现自定义内存分配器
leetcode135. Distribute candy
年薪50w阿裏P8親自下場,教你如何從測試進階
使用AGC重签名服务前后渠道号信息异常分析
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
Greenplum 6.x version change record common manual
注解@ConfigurationProperties的三种使用场景
【MySQL】数据库进阶之触发器内容详解
leetcode134. gas station
Database storage - table partition
LeetCode 715. Range 模块
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
Why choose cloud native database