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.12 - [BOJ] 16194. 카드 구매하기 2 #126

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

21.07.12 - [BOJ] 16194. 카드 구매하기 2 #126

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

Comments

@suhyunsim
Copy link
Owner

문제

핵심 아이디어

  • 카드 N개를 구매하는 비용의 최솟값을 구하는 문제
for (int i=1; i<=n; i++) {
    for (int j=1; j<=i; j++) {
        d[i] = min(d[i],d[i-j]+a[j]);
    }
}
  • D[0] = 0이기 때문에 min으로 풀 수 없음
  • 배열의 초기값을 잘 설정해야 함
      1. 카드의 개수 N <= 1000, 카드팩의 가격 <= 10000 : 초기값을 매우 큰 값으로 초기화
      1. d[i] = -1로 초기화 -> 아직 정답을 구하지 않았다는 뜻

어려운 점, 실수

풀이

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

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 + 1];
        int[] p = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            p[i] = sc.nextInt();
        }
        for (int i = 1; i <= n; i++) {
            d[i] = -1;
            for (int j = 1; j <= i; j++) {
                if (d[i] == -1 || d[i] > d[i - j] + p[j]) {
                    d[i] = d[i - j] + p[j];
                }
            }
        }
        System.out.println(d[n]);
    }
}
@suhyunsim suhyunsim self-assigned this Jul 12, 2021
@suhyunsim suhyunsim added DP 다이나믹 프로그래밍 성공 맞은 문제 실버 BOJ - 실버 labels Jul 12, 2021
@suhyunsim suhyunsim added this to the 7월 3주 차 milestone Jul 12, 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