2916. Subarrays Distinct Element Sum of Squares II Hard

@problem@discussion
#Array#Dynamic Programming#Binary Indexed Tree#Segment Tree



1/**
2 * [2916] Subarrays Distinct Element Sum of Squares II
3 *
4 * You are given a 0-indexed integer array nums.
5 * The distinct count of a subarray of nums is defined as:
6 * 
7 * 	Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].
8 * 
9 * Return the sum of the squares of distinct counts of all subarrays of nums.
10 * Since the answer may be very large, return it modulo 10^9 + 7.
11 * A subarray is a contiguous non-empty sequence of elements within an array.
12 *  
13 * Example 1:
14 * 
15 * Input: nums = [1,2,1]
16 * Output: 15
17 * Explanation: Six possible subarrays are:
18 * [1]: 1 distinct value
19 * [2]: 1 distinct value
20 * [1]: 1 distinct value
21 * [1,2]: 2 distinct values
22 * [2,1]: 2 distinct values
23 * [1,2,1]: 2 distinct values
24 * The sum of the squares of the distinct counts in all subarrays is equal to 1^2 + 1^2 + 1^2 + 2^2 + 2^2 + 2^2 = 15.
25 * 
26 * Example 2:
27 * 
28 * Input: nums = [2,2]
29 * Output: 3
30 * Explanation: Three possible subarrays are:
31 * [2]: 1 distinct value
32 * [2]: 1 distinct value
33 * [2,2]: 1 distinct value
34 * The sum of the squares of the distinct counts in all subarrays is equal to 1^2 + 1^2 + 1^2 = 3.
35 *  
36 * Constraints:
37 * 
38 * 	1 <= nums.length <= 10^5
39 * 	1 <= nums[i] <= 10^5
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-ii/
45// discuss: https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-ii/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn sum_counts(nums: Vec<i32>) -> i32 {
51        0
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_2916() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.