pyassistant

Longest subarray with at most k distinct integers

Given a list of integers nums and an integer k, return the length of the longest contiguous subarray that contains at most k distinct integers. Examples of valid subarrays are contiguous slices of the original list. If k is 0, the answer should be 0. The function should run efficiently for large lists (linear time expected).

Example:

Input:
([1, 2, 1, 2, 3], 2)
Output:
4

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.