-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0862.cpp
More file actions
54 lines (44 loc) · 1.31 KB
/
Copy path0862.cpp
File metadata and controls
54 lines (44 loc) · 1.31 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Priority queue
class Solution {
public:
int shortestSubarray(const vector<int> &nums, int k) const {
const int n = size(nums);
long long acc = 0;
uint res = -1;
using type_t = pair<long long, int>;
priority_queue<type_t, vector<type_t>, greater<>> pq;
for (uint i = 0; i < n; i++) {
acc += nums[i];
if (acc >= k) res = min(res, i + 1);
while (!pq.empty() && acc - pq.top().first >= k) {
res = min(res, i - pq.top().second);
pq.pop();
}
pq.emplace(acc, i);
}
return res;
}
};
// Deque optimization
class Solution {
public:
int shortestSubarray(const vector<int> &nums, int k) const {
static long long prfx[100001];
for (int i = 0; i < size(nums); i++) {
prfx[i + 1] = prfx[i] + nums[i];
}
uint res = -1;
deque<int> dq;
for (uint i = 0; i <= size(nums); i++) {
while (!dq.empty() && prfx[i] - prfx[dq.front()] >= k) {
res = min(res, i - dq.front());
dq.pop_front();
}
while (!dq.empty() && prfx[i] <= prfx[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
return res;
}
};