2389. 和有限的最长子序列

leetcode-2389. 和有限的最长子序列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {
int n = nums.size(), m = queries.size();
sort(nums.begin(), nums.end());
vector<int> f(n + 1);
for (int i = 0; i < n; i++) {
f[i + 1] = f[i] + nums[i];
}
vector<int> answer(m);
for (int i = 0; i < m; i++) {
answer[i] = upper_bound(f.begin(), f.end(), queries[i]) - f.begin() - 1;
}
return answer;
}
};