-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2389.cpp
58 lines (42 loc) · 1.22 KB
/
2389.cpp
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
55
56
57
58
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class Solution
{
public:
vector<int> answerQueries(vector<int> &num, vector<int> &q) {
int n = num.size(), m = q.size();
sort(num.begin(), num.end());
vector<int> prefix(n + 1, 0), ret(m, 0);
for (int i = 0; i < n; ++i) {
prefix[i + 1] = prefix[i] + num[i];
}
for (int k = 0; k < m; k++) {
if (q[k] >= prefix[n]) {
ret[k] = n;
continue;
}
for (int i = 0; i < n; ++i) {
if (num[i] > q[k])
break;
for (int j = i; j <= n; ++j) {
if (j > 0 && num[j - 1] > q[k])
break;
if (prefix[j] - prefix[i] > q[k])
break;
if (ret[k] < j - i) {
ret[k] = j - i;
}
}
}
}
return ret;
}
};
int main(int argc, char const *argv[]) {
Solution s;
vector<int> nums = {736411, 184882, 914641, 37925, 214915}, queries = {718089, 665450};
s.answerQueries(nums, queries);
return 0;
}