Consistent Log Window
MediumArrays · Google
logs is a list of user ids, one per log entry, in the order the entries were written.
First compute the target frequency: the number of times the *least* frequent user appears in the entire log.
A contiguous window logs[i..j] is consistent if the most frequent user inside that window appears exactly target-frequency times.
Return the length of the longest consistent window, or 0 if logs is empty.
Example 1
- Input:
- logs = [4, 4, 7, 9, 7, 4, 9, 9, 4, 7]
- Output:
- 9
- Explanation
- Across the whole log, 4 appears 4 times while 7 and 9 appear 3 times each, so the target is 3. The first 9 entries contain 4 four times, but
logs[1..9]([4, 7, 9, 7, 4, 9, 9, 4, 7]) has every user exactly 3 times, so its maximum frequency is 3 and its length is 9.
Example 2
- Input:
- logs = [3, 1, 3, 2, 3]
- Output:
- 3
- Explanation
- Users 1 and 2 appear only once, so the target is 1 and a consistent window must not repeat any user. The longest such window is
[1, 3, 2], of length 3.
Example 3
- Input:
- logs = [8]
- Output:
- 1
- Explanation
- The only user appears once, so the target is 1, and the whole one-entry log is consistent.
Constraints
0 <= logs.length <= 2 * 10^51 <= logs[i] <= 10^9