891. Sum of Subsequence Widths Hard

@problem@discussion
#Array#Math#Sorting



1/**
2 * [891] Sum of Subsequence Widths
3 *
4 * The width of a sequence is the difference between the maximum and minimum elements in the sequence.
5 * Given an array of integers nums, return the sum of the widths of all the non-empty subsequences of nums. Since the answer may be very large, return it modulo 10^9 + 7.
6 * A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
7 *  
8 * Example 1:
9 * 
10 * Input: nums = [2,1,3]
11 * Output: 6
12 * Explanation: The subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3].
13 * The corresponding widths are 0, 0, 0, 1, 1, 2, 2.
14 * The sum of these widths is 6.
15 * 
16 * Example 2:
17 * 
18 * Input: nums = [2]
19 * Output: 0
20 * 
21 *  
22 * Constraints:
23 * 
24 * 	1 <= nums.length <= 10^5
25 * 	1 <= nums[i] <= 10^5
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/sum-of-subsequence-widths/
31// discuss: https://leetcode.com/problems/sum-of-subsequence-widths/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn sum_subseq_widths(nums: Vec<i32>) -> i32 {
37        0
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_891() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.