Post

백준_14501 (퇴사)

구현방법

  1. maxIncomeArr 배열에 특정일(i)의 최대 수익을 저장
  2. j + t[j]가 i와 같아질 때, i일의 최대수익과 j일의 최대수익 + j일의 급여를 비교하여 i일의 급여최댓값 수정
  3. i일의 최대수익과 maxIncome 변수를 비교하여 최댓값 수정

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.company.baekjoon._14501;

import java.io.*;
import java.util.Arrays;

public class 한규호_14501 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    int n = Integer.parseInt(br.readLine());
    int[] t = new int[n + 1];
    int[] p = new int[n + 1];
    int[] maxIncomeArr = new int[n + 1]; // 특정일(i)의 최대 수익
    int maxIncome = 0; // 최대 수익

    for (int i = 0; i < n; i++) {
      String[] tp = br.readLine().split(" ");
      t[i] = Integer.parseInt(tp[0]);
      p[i] = Integer.parseInt(tp[1]);
    }

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= i; j++) {
        maxIncomeArr[i] = Math.max(maxIncomeArr[i], maxIncomeArr[j]);
        if (j + t[j] == i) {
          maxIncomeArr[i] = Math.max(maxIncomeArr[i], maxIncomeArr[j] + p[j]);
        }
      }
      maxIncome = Math.max(maxIncome, maxIncomeArr[i]);
    }
    bw.write("t[i] : " + Arrays.toString(t) + "\n");
    bw.write("p[i] : " + Arrays.toString(p) + "\n");
    bw.write("max[i] : " + Arrays.toString(maxIncomeArr) + "\n");
    bw.write(Integer.toString(maxIncome));
    br.close();
    bw.close();
  }
}


문제 링크

This post is licensed under CC BY 4.0 by the author.