Trains the technique from
LeetCode 2537Count the Number of Good SubarraysThis 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.
A log holds the readings readings. A matching pair inside a stretch of neighbouring readings is any two positions of that stretch holding the same reading.
A stretch is rich when it holds at least k matching pairs.
Return how many stretches are rich.
Example 1
Four stretches hold at least two matching pairs. Each of them reaches from somewhere at or before the second reading out to the last, picking up the repeated 3, the repeated 4 and the repeated 2 along the way.
Example 2
Five readings all alike give ten matching pairs, exactly the number wanted, and only the whole log manages it.
Example 3
No two readings are alike, so no stretch holds a matching pair at all.
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 count_good(readings: list[int], k: int) -> int:public long countGood(int[] readings, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.