1273. Window Repeat Tally

Easy1.5 points estimatedSliding WindowHash TableCounting

A streaming service logs the id of the show watched in each session, stored in order in nums. For every block of k consecutive sessions, an analyst wants to know how many different show ids were watched at least twice inside that block.

Return an array with one entry per window, for the windows that start at index 0, 1, ..., nums.length - k in that order. Entry t is the number of distinct values that appear two or more times among nums[t..t+k-1]. Slide a count map across the array and keep a running tally that changes only when a count crosses from one to two or from two to one. Aim for O(n) time and O(k) extra space.

Example 1

Input:
nums = [1,2,1,2,3], k = 3
Output:
[1,1,0]
Explanation:

Windows [1,2,1], [2,1,2] and [1,2,3] contain 1, 1 and 0 values that repeat, giving [1,1,0].

Example 2

Input:
nums = [4,4,4,4], k = 2
Output:
[1,1,1]
Explanation:

Each window [4,4] has one repeated value, so the answer is [1,1,1].

Constraints

1 ≤ k ≤ nums.length ≤ 1000

-109 ≤ nums[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(k)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…