Algorithm

DP / 백준 11053 가장 긴 증가하는 부분 수열

Dear-J 2025. 5. 19. 16:41

 

문제 접근

Longest Increasing Subsequence 알고리즘 문제

 

풀이

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

public class S11053 {
    public static void main(String[] args) throws IOException {
        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());
        }

        int[] dp = new int[N + 1];
        dp[1] = 1;
        int max = 1;

        for (int i = 2; i <= N; i++) {
            dp[i] = 1;
            for (int j = 1; j < i; j++) {
                if (arr[i] > arr[j] && dp[i] <= dp[j]) {
                    dp[i] = dp[j] + 1;
                }
            }
            max = Math.max(max, dp[i]);
        }

        System.out.println(max);
    }
}

'Algorithm' 카테고리의 다른 글

DP / 백준 1912 연속합  (0) 2025.05.20
DP / 백준 14002 가장 긴 증가하는 부분 수열 4  (0) 2025.05.20
DP / 백준 2193 이친수  (0) 2025.05.19
DP / 백준 10844 쉬운 계단 수  (0) 2025.05.19
DP / 백준 15990 1,2,3 더하기 5  (0) 2025.05.18