1498. Number of Subsequences That Satisfy the Given Sum Condition Medium
1/**
2 * [1498] Number of Subsequences That Satisfy the Given Sum Condition
3 *
4 * You are given an array of integers nums and an integer target.
5 * Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 10^9 + 7.
6 *
7 * Example 1:
8 *
9 * Input: nums = [3,5,6,7], target = 9
10 * Output: 4
11 * Explanation: There are 4 subsequences that satisfy the condition.
12 * [3] -> Min value + max value <= target (3 + 3 <= 9)
13 * [3,5] -> (3 + 5 <= 9)
14 * [3,5,6] -> (3 + 6 <= 9)
15 * [3,6] -> (3 + 6 <= 9)
16 *
17 * Example 2:
18 *
19 * Input: nums = [3,3,6,8], target = 10
20 * Output: 6
21 * Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
22 * [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]
23 *
24 * Example 3:
25 *
26 * Input: nums = [2,3,3,4,6,7], target = 12
27 * Output: 61
28 * Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
29 * Number of valid subsequences (63 - 2 = 61).
30 *
31 *
32 * Constraints:
33 *
34 * 1 <= nums.length <= 10^5
35 * 1 <= nums[i] <= 10^6
36 * 1 <= target <= 10^6
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/
42// discuss: https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn num_subseq(nums: Vec<i32>, target: i32) -> i32 {
48 0
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_1498() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.