#2916
Subarrays Distinct Element Sum of Squares II
international master · 2150 · lc hard +32 · verified · 22.8% accepted · 157 likes · top 3%
Description
A 0-indexed integer array nums is given. For any subarray nums[i..j], its distinct count is the number of distinct values it contains.
Return the sum of the squares of the distinct counts over all subarrays of nums, modulo 109 + 7.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1]
Output: 15
Explanation: Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Three possible subarrays are:
[2]: 1 distinct value
[2]: 1 distinct value
[2,2]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
Code
1
2
3