Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.07.15 - [BOJ] 14002. 가장 긴 증가하는 부분 수열 4 #131

Closed
suhyunsim opened this issue Jul 15, 2021 · 0 comments
Closed

21.07.15 - [BOJ] 14002. 가장 긴 증가하는 부분 수열 4 #131

suhyunsim opened this issue Jul 15, 2021 · 0 comments
Assignees
Labels
DP 다이나믹 프로그래밍 골드 BOJ - 골드 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jul 15, 2021

문제

핵심 아이디어

  • D[i] = A[1], ..., A[i]까지 수열이 있을 때 A[i]를 마지막으로 하는 가장 긴 증가하는 부분 수열의 길이
  • V[i] = A[i] 앞에 와야 하는 수의 인덱스. 즉 A[i]의 앞에는 A[V[i]]가 와야 길이가 가장 길다.
    • A[j], A[i] 순서 -> V[i] = j
  • 재귀
  • 표 그리는 방식을 그대로 구현
    스크린샷, 2021-07-15 23-08-44

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q14002;

import java.util.Scanner;

public class Main {
    static int [] a;
    static int [] d;
    static int [] v;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        d = new int[n];
        v = new int[n];
        for (int i = 0; i < n; i++) {
            d[i] = 1;
            v[i] = -1;
            for (int j = 0; j < i; j++) {
                if (a[j] < a[i] && d[i] < d[j] + 1) {
                    d[i] = d[j] + 1;
                    v[i] = j;
                }
            }
        }
        int ans = d[0];
        int p = 0;
        for (int i = 0; i < n; i++) {
            if (ans < d[i]) {
                ans = d[i];
                p = i;
            }
        }
        System.out.println(ans);
        go(p);
        System.out.println();
    }

    private static void go(int p) {
        if (p == -1) return;
        go(v[p]);
        System.out.println(a[p] + " ");
    }
}
@suhyunsim suhyunsim added 골드 BOJ - 골드 DP 다이나믹 프로그래밍 labels Jul 15, 2021
@suhyunsim suhyunsim added this to the 7월 3주 차 milestone Jul 15, 2021
@suhyunsim suhyunsim self-assigned this Jul 15, 2021
@suhyunsim suhyunsim added the 실패 시도했지만 맞지 못한 문제 label Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DP 다이나믹 프로그래밍 골드 BOJ - 골드 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant