FunctionRacerProblemsRace

Best Window of Distinct Numbers

EasyTwo pointersPython, JavaScript, TypeScript, Java, C++

Look at every stretch of exactly k consecutive items of nums in which all k numbers are different. Return the largest sum among those stretches.

If no stretch qualifies, return 0.

Example 1

nums [1,5,4,2,9,9,9]
k 3
returns 15

[4, 2, 9] has distinct items and sum 15.

Example 2

nums [4,4,4]
k 3
returns 0

No window of 3 has distinct items.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems