You are given a sorted integer array nums and an integer k.
Return an array such that each distinct element appears at most k times, while preserving the relative order of the elements in nums.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,1,2,2,3]
Explanation:
Each element can appear at most 2 times.
Thus, the resulting array is [1, 1, 2, 2, 3].
Example 2:
Input: nums = [1,2,3], k = 1
Output: [1,2,3]
Explanation:
All elements are distinct and already appear at most once, so the array remains unchanged.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100nums is sorted in non-decreasing order.1 <= k <= nums.lengthFollow-up:
Loading editor...
[1,1,1,2,2,3] 2