Trains the technique from
LeetCode 2389Longest Subsequence With Limited SumThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Parcel weights are given as nums, and each entry of queries is a weight allowance.
For each allowance, work out the largest number of parcels that can be chosen so their weights total at most that allowance. Parcels may be chosen from anywhere in the list.
Return those counts, one per allowance, in the order the allowances are given.
Example 1
Sorted, the weights run 4, 9, 17, 23 with running totals 4, 13, 30, 53. An allowance of 30 reaches three parcels, one of 4 reaches one, one of 3 reaches none, and one of 53 takes all four.
Example 2
Every parcel weighs one, so each allowance fits that many parcels, capped at the five available.
Example 3
Even the lightest parcel is over the allowance, so none fit.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def answer_queries(nums: list[int], queries: list[int]) -> list[int]:public int[] answerQueries(int[] nums, int[] queries)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.