forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-positive-sum-subarray.cpp
More file actions
30 lines (28 loc) · 926 Bytes
/
minimum-positive-sum-subarray.cpp
File metadata and controls
30 lines (28 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Time: O(nlogn)
// Space: O(n)
// prefix sum, two pointers, sliding window, bst, binary search
class Solution {
public:
int minimumSumSubarray(vector<int>& nums, int l, int r) {
static const int INF = numeric_limits<int>::max();
vector<int> prefix(size(nums) + 1);
for (int i = 0; i < size(nums); ++i) {
prefix[i + 1] = prefix[i] + nums[i];
}
int result = INF;
multiset<int> bst;
for (int i = 0; i < size(nums); ++i) {
if (i - l + 1 >= 0) {
bst.emplace(prefix[i - l + 1]);
}
if (i - r >= 0) {
bst.erase(bst.find(prefix[i - r]));
}
const auto it = bst.lower_bound(prefix[i + 1]);
if (it != begin(bst)) {
result = min(result, prefix[i + 1] - *prev(it));
}
}
return result != INF ? result : -1;
}
};