Given an array of integers, find the longest subarray that contains at most K distinct integers and return its length.
Example
Input: nums =[1, 2, 1, 2, 3], k =2Output: 4 Explanation:the subarray nums[0...3] contains 2 distinct values: [1, 2] and is the longest subarray
Note:
Your algorithm should run in O(n^2) time and use O(n) extra space.
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Solution #1 · O(n^2)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check