Skip to content

Commit 763a295

Browse files
committed
3940. Limit Occurrences in Sorted Array
1 parent a7d5803 commit 763a295

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

leetcode3/이진희/3940_Limit Occurrences in Sorted Array.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
/*
1+
/* 2차 풀이
2+
3+
1. 아이디어 : 정렬된 배열을 순회하며, 현재 값 개수가 k 이상이면 continue, 이하면 붙인다 (이전 풀이와 동일)
4+
5+
2. 시간복잡도 : O(N)
6+
7+
3. 자료구조/알고리즘 : 완전탐색
8+
9+
*/
10+
11+
class Solution {
12+
public int[] limitOccurrences(int[] nums, int k) {
13+
int cnt = 1;
14+
int prev = nums[0];
15+
int len = 0;
16+
int[] ans = new int[nums.length];
17+
18+
ans[len++] = nums[0];
19+
20+
for(int i=1; i<nums.length; i++) {
21+
if(prev != nums[i]) {
22+
cnt = 1;
23+
prev = nums[i];
24+
}
25+
else cnt++;
26+
27+
if(cnt>k) continue;
28+
ans[len++] = nums[i];
29+
}
30+
31+
return Arrays.copyOfRange(ans, 0, len);
32+
}
33+
}
34+
35+
/* 1차 풀이
236
337
1. 아이디어 : 배열을 순회하며, 현재 값 개수가 k 이상이면 continue, 이하면 붙인다
438

0 commit comments

Comments
 (0)