You are given an integer array nums of length n, and an integer array queries of length m.
Return an arrayanswerof lengthmwhereanswer[i]is the maximum size of a subsequence that you can take fromnumssuch that the sum of its elements is less than or equal toqueries[i].
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Input: nums =[4,5,2,1], queries =[3,10,21]Output: [2,3,4]Explanation: We answer the queries as follows:- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2is the maximum size of such a subsequence, so answer[0]=2.- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3is the maximum size of such a subsequence, so answer[1]=3.- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4is the maximum size of such a subsequence, so answer[2]=4.
Example 2:
1
2
3
Input: nums =[2,3,4,5], queries =[1]Output: [0]Explanation: The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0]=0.
To maximize the subsequence size under a sum limit, always pick the smallest numbers first. Sorting nums and using prefix sums allows us to quickly find, for each query, the largest prefix whose sum does not exceed the query value.
classSolution {
public: vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {
sort(nums.begin(), nums.end());
int n = nums.size();
vector<int> pre(n +1, 0), ans;
for (int i =0; i < n; ++i)
pre[i +1] = pre[i] + nums[i];
for (int q : queries) {
int l =0, r = n;
while (l < r) {
int m = (l + r +1) /2;
if (pre[m] <= q) l = m;
else r = m -1;
}
ans.push_back(l);
}
return ans;
}
};
classSolution {
publicint[]answerQueries(int[] nums, int[] queries) {
Arrays.sort(nums);
int n = nums.length;
int[] pre =newint[n + 1];
for (int i = 0; i < n; ++i)
pre[i + 1]= pre[i]+ nums[i];
int[] ans =newint[queries.length];
for (int i = 0; i < queries.length; ++i) {
int l = 0, r = n;
while (l < r) {
int m = (l + r + 1) / 2;
if (pre[m]<= queries[i]) l = m;
else r = m - 1;
}
ans[i]= l;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funanswerQueries(nums: IntArray, queries: IntArray): IntArray {
nums.sort()
val pre = IntArray(nums.size + 1)
for (i in nums.indices) {
pre[i + 1] = pre[i] + nums[i]
}
return queries.map { q ->var l = 0var r = nums.size
while (l < r) {
val m = (l + r + 1) / 2if (pre[m] <= q) l = m else r = m - 1 }
l
}.toIntArray()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defanswerQueries(self, nums: list[int], queries: list[int]) -> list[int]:
nums.sort()
pre = [0]
for x in nums:
pre.append(pre[-1] + x)
ans: list[int] = []
n = len(nums)
for q in queries:
l, r =0, n
while l < r:
m = (l + r +1) //2if pre[m] <= q:
l = m
else:
r = m -1 ans.append(l)
return ans
impl Solution {
pubfnanswer_queries(nums: Vec<i32>, queries: Vec<i32>) -> Vec<i32> {
letmut nums = nums;
nums.sort();
let n = nums.len();
letmut pre = vec![0; n +1];
for i in0..n {
pre[i +1] = pre[i] + nums[i];
}
queries.iter().map(|&q| {
let (mut l, mut r) = (0, n);
while l < r {
let m = (l + r +1) /2;
if pre[m] <= q {
l = m;
} else {
r = m -1;
}
}
l asi32 }).collect()
}
}