Skip to content

Commit 376d49f

Browse files
Merge pull request #1917 from CodingTestStudy2/염혜정
[염혜정] Day28
2 parents b8563a2 + 23a0793 commit 376d49f

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// o(n)
2+
3+
class Solution {
4+
public int[] limitOccurrences(int[] nums, int k) {
5+
int n = nums.length, idx = 0;
6+
int[] res = new int[n];
7+
int i = 0;
8+
while (i < n) {
9+
int j = i;
10+
while (j < n && nums[j] == nums[i]) j++;
11+
int count = Math.min(j - i, k);
12+
for (int c = 0; c < count; c++) res[idx++] = nums[i];
13+
i = j;
14+
}
15+
return java.util.Arrays.copyOf(res, idx);
16+
}
17+
}

0 commit comments

Comments
 (0)