Trains the technique from
LeetCode 2913Subarrays Distinct Element Sum of Squares IThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
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.
Example 1
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
The two single readings each have variety 1, and the whole pair has variety 2, giving 1 plus 1 plus 4.
Example 3
The pair holds one distinct reading, so all three stretches have variety 1.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def sum_counts(nums: list[int]) -> int:public int sumCounts(int[] nums)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.