Given a list nums sorted in non-decreasing order and an integer k >= 1, rewrite nums in place so that every value appears at most k times, keeping the relative order. Return the new length n; only the first n slots of nums matter afterwards.
The tests use kept(nums, k), which calls keep_at_most(nums, k) and returns the pair (n, nums[:n]), so they check both the returned length and the kept prefix.
Examples
Input: nums = [3, 3, 6, 6, 6, 9], k = 2
Output: 5, and nums starts with [3, 3, 6, 6, 9]
Input: nums = [4, 4, 4, 4], k = 1
Output: 1, and nums starts with [4]
Constraints
0 <= len(nums) <= 10**5,1 <= k <= 10**5- Target: O(n) time, O(1) extra space (no new list).
Goals
- Use a slow write pointer and a fast read pointer
- Decide whether to keep an element by looking back k slots in the written prefix