Sliding window · Medium
An activity panel highlights the longest uninterrupted run without repeating an event kind.
Return the length of the longest contiguous subarray containing distinct kinds.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ kinds: string[] }
Input rules
- Strings are case-sensitive event identities.
What each field means
kinds— event-kind strings in chronological order. Equal strings represent the same kind.
Output
Return value and rules
- Return the length of the longest consecutive section with no repeated kind.
- You may not skip events inside the section.
- Return
0for an empty event list.
Constraints
- 0 <= kinds.length <= 100000; kind length <= 40 ASCII characters
- A subsequence that skips events is not a valid window.
Examples
Example 1
{
"kinds": [
"a",
"b",
"a",
"c"
]
}Returns
3Example 2
{
"kinds": []
}Returns
0Required complexity
O(n) time and O(k) space