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.22 - [BOJ] 11722. 가장 긴 감소하는 부분 수열 #142

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

21.07.22 - [BOJ] 11722. 가장 긴 감소하는 부분 수열 #142

suhyunsim opened this issue Jul 22, 2021 · 0 comments
Assignees
Labels
DP 다이나믹 프로그래밍 성공 맞은 문제 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

문제

핵심 아이디어

  • 방법1: 가장 긴 증가하는 수열 그대로 이용
    • D[i]: A[i]에서 끝나는 가장 긴 감소하는 부분 수열의 길이
    • 부등호만 반대로 풀이
  • 방법2: 새로운 점화식 이용
    • D[i]: A[i]에서 시작하는 가장 긴 감소하는 부분 수열의 길이
    • i < j, A[i] > A[j]
    • D[i] = max(D[j]) + 1
    • N -> 1

어려운 점, 실수

풀이

방법1: 등호만 다르게

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

import java.util.Scanner;

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

방법2: 새롭게

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        int[] d = new int[n+1];
        for (int i=1; i<=n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i=n; i>=1; i--) {
            d[i] = 1;
            for (int j=i+1; j<=n; j++) {
                if (a[i] > a[j] && d[i] < d[j]+1) {
                    d[i] = d[j]+1;
                }
            }
        }
        int ans = d[1];
        for (int i=2; i<=n; i++) {
            if (ans < d[i]) {
                ans = d[i];
            }
        }
        System.out.println(ans);
    }
}
@suhyunsim suhyunsim self-assigned this Jul 22, 2021
@suhyunsim suhyunsim added DP 다이나믹 프로그래밍 성공 맞은 문제 실버 BOJ - 실버 labels Jul 22, 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