The distinct count of a subarray of nums is defined as:
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].
Return _the sum of thesquares of distinct counts of all subarrays of _nums.
Since the answer may be very large, return it modulo10^9 + 7.
A subarray is a contiguous non-empty sequence of elements within an array.
Input: nums =[1,2,1]Output: 15Explanation: 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.
Input: nums =[2,2]Output: 3Explanation: 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.
For each subarray, the number of distinct elements can be tracked using a sliding window and a hash map. To efficiently compute the sum of squares of distinct counts for all subarrays, we can use a contribution method: for each position, count how many subarrays have a given element as a new unique value, and sum up the squares accordingly.
#include<vector>#include<unordered_map>usingnamespace std;
classSolution {
public:int sumCounts(vector<int>& nums) {
constint MOD =1e9+7;
int n = nums.size();
longlong ans =0;
for (int l =0; l < n; ++l) {
unordered_map<int, int> freq;
int distinct =0;
for (int r = l; r < n; ++r) {
if (++freq[nums[r]] ==1) ++distinct;
ans = (ans +1LL* distinct * distinct) % MOD;
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funcsumCounts(nums []int) int {
mod:= int(1e9+7)
n:= len(nums)
ans:=0forl:=0; l < n; l++ {
freq:=map[int]int{}
distinct:=0forr:=l; r < n; r++ {
freq[nums[r]]++iffreq[nums[r]] ==1 {
distinct++ }
ans = (ans+distinct*distinct) %mod }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
classSolution {
publicintsumCounts(int[] nums) {
int MOD = 1_000_000_007, n = nums.length;
long ans = 0;
for (int l = 0; l < n; ++l) {
Map<Integer, Integer> freq =new HashMap<>();
int distinct = 0;
for (int r = l; r < n; ++r) {
freq.put(nums[r], freq.getOrDefault(nums[r], 0) + 1);
if (freq.get(nums[r]) == 1) ++distinct;
ans = (ans + 1L * distinct * distinct) % MOD;
}
}
return (int)ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
funsumCounts(nums: IntArray): Int {
val MOD = 1_000_000_007
val n = nums.size
var ans = 0Lfor (l in0 until n) {
val freq = mutableMapOf<Int, Int>()
var distinct = 0for (r in l until n) {
freq[nums[r]] = freq.getOrDefault(nums[r], 0) + 1if (freq[nums[r]] ==1) distinct++ ans = (ans + 1L * distinct * distinct) % MOD
}
}
return ans.toInt()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
defsumCounts(nums):
MOD =10**9+7 n = len(nums)
ans =0for l in range(n):
freq = {}
distinct =0for r in range(l, n):
freq[nums[r]] = freq.get(nums[r], 0) +1if freq[nums[r]] ==1:
distinct +=1 ans = (ans + distinct * distinct) % MOD
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use std::collections::HashMap;
pubfnsum_counts(nums: Vec<i32>) -> i32 {
let modulo =1_000_000_007;
let n = nums.len();
letmut ans =0i64;
for l in0..n {
letmut freq = HashMap::new();
letmut distinct =0;
for r in l..n {
*freq.entry(nums[r]).or_insert(0) +=1;
if*freq.get(&nums[r]).unwrap() ==1 { distinct +=1; }
ans = (ans + (distinct * distinct) asi64) % modulo;
}
}
ans asi32}