Skip to content

Commit

Permalink
New Problem Solution - "1856. Maximum Subarray Min-Product"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed May 9, 2021
1 parent f2c22c7 commit 2525f8a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1856|[Maximum Subarray Min-Product](https://leetcode.com/problems/maximum-subarray-min-product/) | [C++](./algorithms/cpp/maximumSubarrayMinProduct/MaximumSubarrayMinProduct.cpp)|Medium|
|1855|[Maximum Distance Between a Pair of Values](https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/) | [C++](./algorithms/cpp/maximumDistanceBetweenAPairOfValues/MaximumDistanceBetweenAPairOfValues.cpp)|Medium|
|1854|[Maximum Population Year](https://leetcode.com/problems/maximum-population-year/) | [C++](./algorithms/cpp/maximumPopulationYear/MaximumPopulationYear.cpp)|Easy|
|1851|[Minimum Interval to Include Each Query](https://leetcode.com/problems/minimum-interval-to-include-each-query/) | [C++](./algorithms/cpp/minimumIntervalToIncludeEachQuery/MinimumIntervalToIncludeEachQuery.cpp)|Hard|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Source : https://leetcode.com/problems/maximum-subarray-min-product/
// Author : Hao Chen
// Date : 2021-05-09

/*****************************************************************************************************
*
* The min-product of an array is equal to the minimum value in the array multiplied by the array's
* sum.
*
* For example, the array [3,2,5] (minimum value is 2) has a min-product of 2 * (3+2+5) = 2 *
* 10 = 20.
*
* Given an array of integers nums, return the maximum min-product of any non-empty subarray of nums.
* Since the answer may be large, return it modulo 10^9 + 7.
*
* Note that the min-product should be maximized before performing the modulo operation. Testcases are
* generated such that the maximum min-product without modulo will fit in a 64-bit signed integer.
*
* A subarray is a contiguous part of an array.
*
* Example 1:
*
* Input: nums = [1,2,3,2]
* Output: 14
* Explanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).
* 2 * (2+3+2) = 2 * 7 = 14.
*
* Example 2:
*
* Input: nums = [2,3,3,1,2]
* Output: 18
* Explanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).
* 3 * (3+3) = 3 * 6 = 18.
*
* Example 3:
*
* Input: nums = [3,1,5,6,4,2]
* Output: 60
* Explanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).
* 4 * (5+6+4) = 4 * 15 = 60.
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 10^7
******************************************************************************************************/

class Solution {
public:
int maxSumMinProduct(vector<int>& nums) {
nums.push_back(0); //edge case

//prefix sum
vector<long> sums(nums.size(), 0);
// sums[i] = sum (num[0], num[1], num[2], ... num[n-1])
// sums[m] - sums[n] = sum (num[n], sum[n+1] .... nums[m-1]); m > n
for(int i=0; i<nums.size()-1; i++) {
sums[i+1] = sums[i] + nums[i];
}

stack<int> s;
long m = 0;
for(int i=0; i<nums.size(); i++) {
while( !s.empty() && nums[s.top()] > nums[i]) {
int min = nums[s.top()]; s.pop();
int start = s.empty() ? 0 : s.top() + 1;
int end = i;
m = max(m , min * (sums[end] - sums[start]));

// cout << "[";
// for(int k = start; k < end-1; k++) {
// cout << nums[k] << ",";
// }
// cout << nums[end-1] << "], " << min << "*" << (sums[end] - sums[start])
// << "=" << min * (sums[end] - sums[start]) << endl;

}
// if the num is increasing, then push into stack
s.push(i);
}
//cout << endl;
return m % 1000000007;

}
};

0 comments on commit 2525f8a

Please sign in to comment.