Skip to content

Conversation

@sunha20
Copy link
Contributor

@sunha20 sunha20 commented Jan 9, 2026

🚀 이슈 번호

Resolve: {#2293}

🧩 문제 해결

스스로 해결: ✅ / ❌

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 LCS
  • 🔹 어떤 방식으로 접근했는지

규칙을 찾다보니, 양 끝에서 부터 공통된 수에 영향을 받음.
그중에서도 공통된 가장 긴 수열을 찾으면 되기 때문에 LCS 사

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(N^2)
  • 이유: 이중 포문

💻 구현 코드

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

public class Main {
    public static void main(String[] args) throws IOException {
        // 입력을 빠르게 받기 위해 BufferedReader 사용
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int N = Integer.parseInt(br.readLine());
        int[] arr = new int[N + 1];
        
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i = 1; i <= N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        // dp[i][j]: 원래 수열의 i번째까지와 뒤집은 수열의 j번째까지의 LCS 길이
        // 메모리 제한(128MB)이 빡빡하므로 int[][] 사용 시 주의 필요하지만, 5000*5000 int는 약 100MB로 통과 가능
        int[][] dp = new int[N + 1][N + 1];

        for(int i = 1; i <= N; i++) {
            for(int j = 1; j <= N; j++) {
                // arr[i]와 뒤집은 수열의 j번째 문자를 비교
                // 뒤집은 수열의 j번째 문자는 원래 수열의 (N - j + 1)번째 문자와 같음
                if(arr[i] == arr[N - j + 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        // 전체 길이 - 가장 긴 팰린드롬 부분 수열의 길이(LCS)
        System.out.println(N - dp[N][N]);
    }
}

@sunha20 sunha20 self-assigned this Jan 9, 2026
@sunha20 sunha20 linked an issue Jan 9, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260109 : 코딩테스트

2 participants