Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_20922] 겹치는 건 싫어 본문

Study/Problem Solving

[BOJ_20922] 겹치는 건 싫어

monitor 2023. 11. 11. 18:03

설명


투 포인터로 접근하였고 처음 숫자의 크기만큼 배열을 짜서 각 숫자마다 가진 갯수를 뒤지는 식으로 풀었다. 사실 구현 할 때 맵 형태로 구현 해보았으나 생각보다 맵을 써서 푸는 것 보다 배열이 메모리나 속도 측면에서 더 빨랐다.

 

코드


package two_pointer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_20922 {
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine()," ");

        int N = Integer.parseInt(tk.nextToken());
        int K = Integer.parseInt(tk.nextToken());
        int arr[] = new int[N];
        int count[] = new int[100001];
        tk = new StringTokenizer(in.readLine()," ");
        for(int i = 0; i < N; i++){
            arr[i] = Integer.parseInt(tk.nextToken());
        }
        int start = 0;
        int end = 0;
        int ans = 0;
        while(start < N){
            while(end < N && count[arr[end]] < K){
                count[arr[end]]++;
                end++;
            }
            ans = Math.max(end - start,ans);
            count[arr[start]]--;
            start++;
        }
        System.out.println(ans);
    }
}
 

20922번: 겹치는 건 싫어

홍대병에 걸린 도현이는 겹치는 것을 매우 싫어한다. 특히 수열에서 같은 원소가 여러 개 들어 있는 수열을 싫어한다. 도현이를 위해 같은 원소가 $K$개 이하로 들어 있는 최장 연속 부분 수열

www.acmicpc.net

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_22862] 가장 긴 짝수 연속한 부분 수열 (large)  (0) 2023.11.13
[BOJ_2470] 두 용액  (1) 2023.11.12
[BOJ_21921] 블로그  (0) 2023.11.08
[BOJ_11728] 배열 합치기  (0) 2023.11.07
[BOJ_7662] 이중 우선순위 큐  (2) 2023.11.07