From 763a2950c672c3198f505b8aedebacaefe27ccb9 Mon Sep 17 00:00:00 2001 From: SinnoLn Date: Wed, 26 Aug 2026 12:22:27 +0900 Subject: [PATCH] 3940. Limit Occurrences in Sorted Array --- ...40_Limit Occurrences in Sorted Array.java" | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/3940_Limit Occurrences in Sorted Array.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/3940_Limit Occurrences in Sorted Array.java" index 42ed95b6..f49e72a3 100644 --- "a/leetcode3/\354\235\264\354\247\204\355\235\254/3940_Limit Occurrences in Sorted Array.java" +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/3940_Limit Occurrences in Sorted Array.java" @@ -1,4 +1,38 @@ -/* +/* 2차 풀이 + +1. 아이디어 : 정렬된 배열을 순회하며, 현재 값 개수가 k 이상이면 continue, 이하면 붙인다 (이전 풀이와 동일) + +2. 시간복잡도 : O(N) + +3. 자료구조/알고리즘 : 완전탐색 + + */ + +class Solution { + public int[] limitOccurrences(int[] nums, int k) { + int cnt = 1; + int prev = nums[0]; + int len = 0; + int[] ans = new int[nums.length]; + + ans[len++] = nums[0]; + + for(int i=1; ik) continue; + ans[len++] = nums[i]; + } + + return Arrays.copyOfRange(ans, 0, len); + } +} + +/* 1차 풀이 1. 아이디어 : 배열을 순회하며, 현재 값 개수가 k 이상이면 continue, 이하면 붙인다