3351. Sum of Good Subsequences Hard

@problem@discussion
#Array#Hash Table#Dynamic Programming



1/**
2 * [3351] Sum of Good Subsequences
3 *
4 * You are given an integer array nums. A good <span data-keyword="subsequence-array">subsequence</span> is defined as a subsequence of nums where the absolute difference between any two consecutive elements in the subsequence is exactly 1.
5 * Return the sum of all possible good subsequences of nums.
6 * Since the answer may be very large, return it modulo 10^9 + 7.
7 * Note that a subsequence of size 1 is considered good by definition.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [1,2,1]</span>
12 * Output: <span class="example-io">14</span>
13 * Explanation:
14 * 
15 * 	Good subsequences are: [1], [2], [1], [1,2], [2,1], [1,2,1].
16 * 	The sum of elements in these subsequences is 14.
17 * </div>
18 * <strong class="example">Example 2:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [3,4,5]</span>
21 * Output: <span class="example-io">40</span>
22 * Explanation:
23 * 
24 * 	Good subsequences are: [3], [4], [5], [3,4], [4,5], [3,4,5].
25 * 	The sum of elements in these subsequences is 40.
26 * </div>
27 *  
28 * Constraints:
29 * 
30 * 	1 <= nums.length <= 10^5
31 * 	0 <= nums[i] <= 10^5
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/sum-of-good-subsequences/
37// discuss: https://leetcode.com/problems/sum-of-good-subsequences/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn sum_of_good_subsequences(nums: Vec<i32>) -> i32 {
43        0
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_3351() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.