You are given a 0-indexed integer array nums. 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 the squares of distinct counts of all subarrays of nums. A subarray is a contiguous non-empty sequence of elements within an array.
Example 1
Input: nums = [1,2,1]
Output: 15
Explanation: The six subarrays have distinct counts 1, 1, 1, 2, 2, 2, so the sum of squares is 1 + 1 + 1 + 4 + 4 + 4 = 15.
Example 2
Input: nums = [1,1]
Output: 3
Explanation: The three subarrays are [1], [1], and [1,1], each with a distinct count of 1, so the sum of squares is 1 + 1 + 1 = 3.
Example 3
Input: nums = [1,2,3]
Output: 20
Explanation: Distinct counts are 1, 1, 1 for the single elements, 2 and 2 for the pairs, and 3 for the whole array, giving 1 + 1 + 1 + 4 + 4 + 9 = 20.
Fix each start index, extend the end one step at a time, and keep a set of the values seen so far so the distinct count is always just the set's size.
Run checks all cases above. Submit evaluates all test cases.