백준 14002 java : DP, LIS

magicdrill·2025년 1월 20일
0

백준 문제풀이

목록 보기
532/654

백준 14002 java : DP, LIS

import java.util.Scanner;
import java.util.Arrays;
import java.util.Stack;

public class bj14002 {
    static Scanner scanner = new Scanner(System.in);
    static int N;
    static int [] A;

    public static void main(String[] args) {
        inputData();
        findAnswer();

        scanner.close();
    }

    public static void inputData(){
        System.out.println("inputData()");
        int i;

        N = scanner.nextInt();
        A = new int[N + 1];
        for(i = 1; i <= N; i++){
            A[i] = scanner.nextInt();
        }
    }

    public static void findAnswer() {
        System.out.println("findAnswer()");
        int[] DP = new int[N + 1];
        int maxLength = 0;
        int i, j;

        for (i = 1; i <= N; i++) {
            for (j = 0; j < i; j++) {
                if (A[i] > A[j]) {
                    DP[i] = Math.max(DP[i], DP[j] + 1);
                    maxLength = Math.max(maxLength, DP[i]);
                }
            }

            for(j = 0; j < DP.length; j++){
                System.out.print(DP[j] + " ");
            }
            System.out.println();
        }
        System.out.println(maxLength);

        Stack<Integer> stack = new Stack<>();
        int lengthToFind = maxLength;

        for (i = N; i >= 1; i--) {
            if (DP[i] == lengthToFind) {
                stack.push(A[i]);
                lengthToFind--;
            }
        }

        while (!stack.isEmpty()) {
            System.out.print(stack.pop() + " ");
        }
    }
}

0개의 댓글