Class Solution
-
- All Implemented Interfaces:
public final class Solution2389 - Longest Subsequence With Limited Sum\.
Easy
You are given an integer array
numsof lengthn, and an integer arrayqueriesof lengthm.Return an array
answerof 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.
Example 1:
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 2 is the maximum size of such a subsequence, so answer0 = 2.
The subsequence 4,5,1 has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer1 = 3.
The subsequence 4,5,2,1 has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer2 = 4.
Example 2:
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 answer0 = 0.
Constraints:
n == nums.lengthm == queries.length1 <= n, m <= 1000<code>1 <= numsi, queriesi<= 10<sup>6</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntArrayanswerQueries(IntArray nums, IntArray queries)-
-
Method Detail
-
answerQueries
final IntArray answerQueries(IntArray nums, IntArray queries)
-
-
-
-