Class Solution
-
- All Implemented Interfaces:
public final class Solution3685 - Subsequence Sum After Capping Elements.
Medium
You are given an integer array
numsof sizenand a positive integerk.An array capped by value
xis obtained by replacing every elementnums[i]withmin(nums[i], x).For each integer
xfrom 1 ton, determine whether it is possible to choose a subsequence from the array capped byxsuch that the sum of the chosen elements is exactlyk.Return a 0-indexed boolean array
answerof sizen, whereanswer[i]istrueif it is possible when usingx = i + 1, andfalseotherwise.Example 1:
Input: nums = 4,3,2,4, k = 5
Output: false,false,true,true
Explanation:
For
x = 1, the capped array is[1, 1, 1, 1]. Possible sums are1, 2, 3, 4, so it is impossible to form a sum of5.For
x = 2, the capped array is[2, 2, 2, 2]. Possible sums are2, 4, 6, 8, so it is impossible to form a sum of5.For
x = 3, the capped array is[3, 3, 2, 3]. A subsequence[2, 3]sums to5, so it is possible.For
x = 4, the capped array is[4, 3, 2, 4]. A subsequence[3, 2]sums to5, so it is possible.
Example 2:
Input: nums = 1,2,3,4,5, k = 3
Output: true,true,true,true,true
Explanation:
For every value of
x, it is always possible to select a subsequence from the capped array that sums exactly to3.Constraints:
1 <= n == nums.length <= 40001 <= nums[i] <= n1 <= k <= 4000
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleanArraysubsequenceSumAfterCapping(IntArray nums, Integer k)-
-
Method Detail
-
subsequenceSumAfterCapping
final BooleanArray subsequenceSumAfterCapping(IntArray nums, Integer k)
-
-
-
-