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.05.21 - [BOJ] 14501. 퇴사 #73

Closed
suhyunsim opened this issue May 21, 2021 · 0 comments
Closed

21.05.21 - [BOJ] 14501. 퇴사 #73

suhyunsim opened this issue May 21, 2021 · 0 comments
Assignees
Labels
DP 다이나믹 프로그래밍 브루트포스 완전탐색 성공 맞은 문제 실버 BOJ - 실버 재귀

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented May 21, 2021

문제

핵심 아이디어

브루트포스로 풀이

  • go(day, sum)
    • day일에 있는 상담을 할 지 말 지 결정
    • 지금까지 얻은 수익: sum
    • 1일 ~ n일이면 편하니까 하나씩 1 더해서 작성
    • 정답 찾음: day == n + 1
    • 불가능: day > n + 1
    • 상담 O: go(day + t[day], sum + p[day])
    • 상담 X: go(day + 1, sum)
  • O(2^n)

DP로 풀이

  • 작은 문제의 정답이 언제 구해도 같고 작은 문제로 큰 문제를 구할 수 있을 때 브루트포스를 DP로 풀 수 있음
  • 작은 문제의 최대값만 알아놓고 그 다음 날짜부터 구하면 됨(작은 문제 영향 X)

어려운 점, 실수

풀이

브루트포스

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

import java.util.Scanner;

public class Main {
    static int[] t;
    static int[] p;
    static int n;
    static int ans = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        t = new int[n + 1];
        p = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            t[i] = sc.nextInt();
            p[i] = sc.nextInt();
        }
        go(1, 0);
        System.out.println(ans);
    }

    private static void go(int day, int sum) {
        if (day == n + 1) {
            if (ans < sum) ans = sum;
            return;
        }
        if (day > n + 1) {
            return;
        }
        go(day + t[day], sum + p[day]);
        go(day + 1, sum);
    }
}

DP

  • 똑같고 go()에서 한 부분만 수정
    private static void go(int day, int sum) {
        if (day == n + 1) {
            if (ans < sum) ans = sum;
            return;
        }
        if (day > n + 1) {
            return;
        }
        int t1 = go(day + 1);
        int t2 = p[day] + go(day + t[day]);
        d[day] = Math.max(t1, t2);
        return d[day];
//        go(day + t[day], sum + p[day]);
//        go(day + 1, sum);
    }
@suhyunsim suhyunsim added 브루트포스 완전탐색 실버 BOJ - 실버 재귀 labels May 21, 2021
@suhyunsim suhyunsim self-assigned this May 21, 2021
@suhyunsim suhyunsim added 성공 맞은 문제 DP 다이나믹 프로그래밍 labels May 21, 2021
@suhyunsim suhyunsim added this to the 5월 3주 차 milestone May 21, 2021
suhyunsim added a commit that referenced this issue Apr 15, 2022
- DP 풀이 추가

Issue #73
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