We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a8f0fe9 commit 9ea331bCopy full SHA for 9ea331b
C++/maximum-subarray-min-product.cpp
@@ -0,0 +1,25 @@
1
+// Time: O(n)
2
+// Space: O(n)
3
+
4
+class Solution {
5
+public:
6
+ int maxSumMinProduct(vector<int>& nums) {
7
+ static const int MOD = 1e9 + 7;
8
9
+ vector<int64_t> prefix(nums.size() + 1);
10
+ for (int i = 0; i < size(nums); ++i) {
11
+ prefix[i + 1] = prefix[i] + nums[i];
12
+ }
13
+ vector<int> stk = {-1};
14
+ int64_t result = 0;
15
+ for (int i = 0; i <= size(nums); ++i) {
16
+ while (stk.back() != -1 && (i == size(nums) || nums[stk.back()] >= nums[i])) {
17
+ int j = stk.back();
18
+ stk.pop_back();
19
+ result = max(result, nums[j] * (prefix[(i - 1) + 1] - prefix[stk.back() + 1]));
20
21
+ stk.emplace_back(i);
22
23
+ return result % MOD;
24
25
+};
0 commit comments