Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to find the largest subarray of products in LeetCode

Shulou Source: shulou.com Published: 2022-06-02 05:28:51 09月26日 Update

In this issue, the editor will bring you about how to find the largest subarray of products in LeetCode. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The first step is to find the intermediate state: where the intermediate state max_ st [I] represents the maximum product of the subarray at the end of the I element, and min_ st [I] represents the minimum product of the subarray at the end of the I element.

The second step is to determine the state transition: when nums [I] is positive, it is directly multiplied by the maximum product and the minimum product of the previous step, and compared with itself to realize the state transition of the maximum and minimum values of the previous step, otherwise, the current minimum product is obtained by multiplying the maximum value of the previous step and comparing it with itself, and the current maximum value is obtained by multiplying the minimum value of the previous step and comparing it with itself.

Class Solution: def maxProduct (self, nums: List [int])-> int: max_st, min_st = [0] * len (nums), [0] * len (nums) max_st [0], min_st [0] = nums [0], nums [0] for i in range (1 Len (nums): if nums [I] > = 0: max_ STI = max (nums [I], nums [I] * max_ STI [I-1]) min_ STI = min (nums [I], nums [I] * min_ STI [I-1]) else: max_ STI = max (nums [I] Nums [I] * min_ st [I-1]) min_ st [I] = min (nums [I], nums [I] * max_ st [I-1]) return max (max_st)

This is how to find the largest subarray of products in the LeetCode shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Maximum product minimum array state maximum element content analysis professional small and medium rich in content that is articles more positive numbers knowledge articles industries angles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux OPPO Reno Redmi Microsoft