최대 1 분 소요



https://www.acmicpc.net/problem/5883



🔍 문제 풀이

문제 도식화

assets/images/2025/5883.jpg



💻 코드

전체 코드

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Set<Integer> set = new HashSet<>();

        int n = Integer.parseInt(br.readLine());
        int arr[] = new int[n];

        for(int i=0; i<n; i++){
            int x = Integer.parseInt(br.readLine());
            set.add(x);
            arr[i] = x;
        }

        int ans = 0;  // 연속 최장 길이
        for(int k:set){
            int cnt = 0; // 현재 연속 길이
            int prev = -1;
            for(int i=0; i<n; i++){
                if(arr[i] == k) continue; // k 제거(건너뛰기)

                if(arr[i] == prev) {
                    cnt ++;
                    ans = Math.max(ans, cnt);
                }
                else cnt = 0;

                prev = arr[i];
            }

        }

        System.out.println(ans + 1);
    }
}


카테고리:

업데이트:

댓글남기기