What nutritional benefits might veganism offer athletes?

Vegan diet elements are being incorporated into daily meals by athletes of all skill levels, who report considerable improvements in their performance and recovery. In reality, a lot of people now…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




152. Maximum Product Subarray

Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.

A subarray is a contiguous subsequence of the array.

Example 1:

Example 2:

Constraints:

[想法]

If all elements in the array are all positive, no doubt, the largest product is to multiply all elements.
e.g. nums = [1,2,3,4,5] maxProduct = 1*2*3*4*5 = 120

What about if some elements are negative numbers?
e.g. nums = [-1,-2,-3,-4]

If the amount of the negative numbers is even, the result is positive.
If the amount of the negative numbers is odd, the result is negative.

We don’t know how many negative numbers in the array, the best way is to track the current maxProduct and the current minProduct.

curMax = max(n*curMax, n*curMin, n)
curMin = min(n*curMax, n*curMin, n)

Also, we have to compare maxProduct in every iteration and save it to a variable. After iterate all elements, just return the variable.

[Python Code]

[想法2]

在解答區看到的想法,覺得很直覺,所以特地記下來

only negative numbers affect the result.

There are 4 situations:

We can think of -5 as dividing the array into 2 halves, [3,2] and [6,3]. The max product of the forward traversal is 6, and the reverse traversal is 18.

3. the array has even numbers of negative numbers in the array: [3,2,-5,1,7,-4,8,-3,6,-1], the max Product Subarray is to multiply all elements

4. the array has multiple odd numbers of negative numbers:[1,5,-2,4,-6,-3,3]

We can think of the “last” negative number in each traversal breaks the array to 2 halves. In this case , the max array in forwarding traversal is the maximum of ([1,5,-2,4,-6] and [3]) which is 240. In the reverse, the split is sperated by -2. So the max subarray is the maximum of ([4,-6,-3,3] and [1,5])

In order to deal with #2 and #4, we can calculate the max product by forwarding traversal and reversing traversal. Then compare the max product from each way.

There is one edge case : 0 , once 0 appears, everything becomes 0, so we have to treat the next number as a beginning of a new subarray and calculate again.

[Python Code]

Add a comment

Related posts:

Four Steps to Managing a Bad Decision

Everyone makes mistakes. But when you’re in a leadership role, the stakes are higher. Your missteps resonate across teams, and in some cases, entire organizations. Acknowledging mistakes is crucial…

How To React To The Silent Treatment

When someone you care about ignores you and gives you the cold shoulder through their silence, it can be difficult to respond appropriately. It’s normal to feel hurt and frustrated in this situation…

Modern Sideboard Buffets

When it comes to modern home decor, sideboard buffets are becoming increasingly popular as both functional and stylish pieces. These versatile furniture items can serve as storage units, display…