diff --git a/README.md b/README.md index 9e77903a..37166595 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/algorithms/cpp/maximumSubarrayMinProduct/MaximumSubarrayMinProduct.cpp b/algorithms/cpp/maximumSubarrayMinProduct/MaximumSubarrayMinProduct.cpp new file mode 100644 index 00000000..7ab71827 --- /dev/null +++ b/algorithms/cpp/maximumSubarrayMinProduct/MaximumSubarrayMinProduct.cpp @@ -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& nums) { + nums.push_back(0); //edge case + + //prefix sum + vector 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 s; + long m = 0; + for(int i=0; i 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; + + } +};