Longest k-distinct subarray

Write codeMedium25 minMeta style

Your task

Given an integer array a and an integer k, return the length of the longest contiguous subarray that contains at most k distinct values. If k <= 0 or the array is empty, return 0.

Examples

longest_at_most_k_distinct(a: list[int], k: int) -> int
Example 1
Inputlongest_at_most_k_distinct([1, 2, 1, 2, 3], 2)
Output4