Largest Product Stretch and Its Bounds
Problem statement
Given an integer array nums, find a non-empty contiguous stretch nums[start..end] whose product is as large as possible.
If several stretches reach that largest product, choose the shortest one; if several of those are equally short, choose the one with the smallest start.
Return [product, start, end] (indexes from 0, both ends included).
Function
maxProductSubarray(nums: int[]) → long[]Examples
Example 1
nums = [2,-5,3,4,-1]return = [120,0,4]The whole array has two negative values, so its product is positive: 2 × (-5) × 3 × 4 × (-1) = 120. No other stretch beats it, so the answer is [120, 0, 4].
Example 2
nums = [1,3,1,0,3]return = [3,1,1]The largest product is 3. Several stretches reach it: [3] at index 1, [1,3], [3,1], [1,3,1] and [3] at index 4. The shortest have length 1, and the leftmost of those starts at index 1, so the answer is [3, 1, 1].
Example 3
nums = [-4,0,-2]return = [0,1,1]Every stretch with a negative product loses to the single 0, whose product 0 is the largest. The answer is [0, 1, 1].
Constraints
1 <= nums.length <= 2000-10 <= nums[i] <= 10- In every stretch of
numsthat contains no0, the product of the absolute values is at most10^18, so every product fits in a 64-bit integer.