← 返回 oracle 的题目列表Remove Duplicates from Sorted Array with At Most K Occurrences
类型:online_judge
Given a non-decreasing sorted integer array nums and an integer k, remove duplicates in place so that every distinct value appears at most k times.
Return the resulting length m. The first m positions of nums must contain the retained values in their original relative order.
Requirements:
Time: O(n)
Extra space: O(1)
Input
Line 1: n k
Line 2: n sorted integers
Output
Line 1: m
Line 2: the first m retained elements.
Example
Input
8 2
1 1 1 2 2 3 3 3
Output
6
1 1 2 2 3 3