|
| 1 | +// Source : https://leetcode.com/problems/maximum-subarray-min-product/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-05-09 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * The min-product of an array is equal to the minimum value in the array multiplied by the array's |
| 8 | + * sum. |
| 9 | + * |
| 10 | + * For example, the array [3,2,5] (minimum value is 2) has a min-product of 2 * (3+2+5) = 2 * |
| 11 | + * 10 = 20. |
| 12 | + * |
| 13 | + * Given an array of integers nums, return the maximum min-product of any non-empty subarray of nums. |
| 14 | + * Since the answer may be large, return it modulo 10^9 + 7. |
| 15 | + * |
| 16 | + * Note that the min-product should be maximized before performing the modulo operation. Testcases are |
| 17 | + * generated such that the maximum min-product without modulo will fit in a 64-bit signed integer. |
| 18 | + * |
| 19 | + * A subarray is a contiguous part of an array. |
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * |
| 23 | + * Input: nums = [1,2,3,2] |
| 24 | + * Output: 14 |
| 25 | + * Explanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2). |
| 26 | + * 2 * (2+3+2) = 2 * 7 = 14. |
| 27 | + * |
| 28 | + * Example 2: |
| 29 | + * |
| 30 | + * Input: nums = [2,3,3,1,2] |
| 31 | + * Output: 18 |
| 32 | + * Explanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3). |
| 33 | + * 3 * (3+3) = 3 * 6 = 18. |
| 34 | + * |
| 35 | + * Example 3: |
| 36 | + * |
| 37 | + * Input: nums = [3,1,5,6,4,2] |
| 38 | + * Output: 60 |
| 39 | + * Explanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4). |
| 40 | + * 4 * (5+6+4) = 4 * 15 = 60. |
| 41 | + * |
| 42 | + * Constraints: |
| 43 | + * |
| 44 | + * 1 <= nums.length <= 10^5 |
| 45 | + * 1 <= nums[i] <= 10^7 |
| 46 | + ******************************************************************************************************/ |
| 47 | + |
| 48 | +class Solution { |
| 49 | +public: |
| 50 | + int maxSumMinProduct(vector<int>& nums) { |
| 51 | + nums.push_back(0); //edge case |
| 52 | + |
| 53 | + //prefix sum |
| 54 | + vector<long> sums(nums.size(), 0); |
| 55 | + // sums[i] = sum (num[0], num[1], num[2], ... num[n-1]) |
| 56 | + // sums[m] - sums[n] = sum (num[n], sum[n+1] .... nums[m-1]); m > n |
| 57 | + for(int i=0; i<nums.size()-1; i++) { |
| 58 | + sums[i+1] = sums[i] + nums[i]; |
| 59 | + } |
| 60 | + |
| 61 | + stack<int> s; |
| 62 | + long m = 0; |
| 63 | + for(int i=0; i<nums.size(); i++) { |
| 64 | + while( !s.empty() && nums[s.top()] > nums[i]) { |
| 65 | + int min = nums[s.top()]; s.pop(); |
| 66 | + int start = s.empty() ? 0 : s.top() + 1; |
| 67 | + int end = i; |
| 68 | + m = max(m , min * (sums[end] - sums[start])); |
| 69 | + |
| 70 | + // cout << "["; |
| 71 | + // for(int k = start; k < end-1; k++) { |
| 72 | + // cout << nums[k] << ","; |
| 73 | + // } |
| 74 | + // cout << nums[end-1] << "], " << min << "*" << (sums[end] - sums[start]) |
| 75 | + // << "=" << min * (sums[end] - sums[start]) << endl; |
| 76 | + |
| 77 | + } |
| 78 | + // if the num is increasing, then push into stack |
| 79 | + s.push(i); |
| 80 | + } |
| 81 | + //cout << endl; |
| 82 | + return m % 1000000007; |
| 83 | + |
| 84 | + } |
| 85 | +}; |
0 commit comments