当前位置:网站首页>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 !

原网站

版权声明
本文为[Encounter simulation volume]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070617362799.html