Solve a coding challenge on a whiteboard or coding platform for Beyondmath.
Implement longest_subarray_at_most_k_distinct(nums, k). Given an integer list and k, return the length of the longest contiguous subarray containing at most k distinct values.
The function returns an integer, and an empty result is valid when k is zero or the input is empty. Constraints: 0 <= len(nums) <= 10^4, 0 <= k <= 10^4, and each value is between -10^9 and 10^9.
def longest_subarray_at_most_k_distinct(nums, k):