[BOJ 15486] ํด์ฌ2
1. ๋ฌธ์ : Link
์ต๋ ์์ต ๋น๊ต
2. ํ์ด
Math.max(max, dp[i]) ํด์ฃผ๋ ์ด์ -> ์ด์ ๊น์ง์ max๊ฐ๊ณผ ์ง๊ธ ํ์ฌ dp ๊ฐ ์ค ์ต๋๊ฐ์ ํด๋น ์ง์ ์ ๋ํด์ฃผ๊ธฐ ์ํด
(
3. ์ฝ๋
import java.io.IOException;
import java.util.*;
public class Main {
static int[] t;
static int[] p;
static int[] dp;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
t = new int[n + 2];
p = new int[n + 2];
dp = new int[n + 2];
for (int i = 1; i <= n; i++) {
t[i] = sc.nextInt();
p[i] = sc.nextInt();
}
int max = Integer.MIN_VALUE;
for (int i = 1; i <= n + 1; i++) {
max = Math.max(max, dp[i]);
if (i + t[i] < n + 2 && (max + p[i] > dp[i + t[i]])) {
dp[i + t[i]] = max + p[i];
}
}
System.out.println(max);
}
}
'๐ป Coding Problems Solving > Dynamic Programming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฉ๋ฆฌ ๋ฐ๊ธฐ (์๋ฐ java) (0) | 2023.01.12 |
---|---|
[LeetCode] Maximum Subarray (0) | 2022.07.24 |
[LeetCode] Climbing Stairs (0) | 2022.07.09 |
[BOJ 2294] ๋์ 2 (0) | 2022.06.29 |
[BOJ 2293] ๋์ 1 (0) | 2022.06.29 |
์ต๊ทผ๋๊ธ