Subarrays Distinct Element Sum of Squares I
Real-world version: Squared Variety Over Every WindowThe statement here is our own: the same inputs, outputs and technique.
Readings are given as nums. For a contiguous stretch of them, its variety is how many distinct readings it holds.
Return the total of the squares of the varieties over every non-empty contiguous stretch.
Examples
Example 1
- Input
- nums = [4, 9, 4, 7, 9]
- Output
- 70
Every one of the fifteen stretches contributes the square of its own variety, and repeats inside a stretch add nothing, which is what keeps this total below what all-distinct readings would give.
Example 2
- Input
- nums = [3, 8]
- Output
- 6
The two single readings each have variety 1, and the whole pair has variety 2, giving 1 plus 1 plus 4.
Example 3
- Input
- nums = [3, 3]
- Output
- 3
The pair holds one distinct reading, so all three stretches have variety 1.
Constraints
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def sum_counts(nums: list[int]) -> int:public int sumCounts(int[] nums)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's