BackJoon Algorithm 계단 오르기 2579 (Java)
BackJoon Algorithm - Java
문제
풀이
- dp문제는 어렵다..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Back_2579 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] DP = new int[N + 1];
int[] arr = new int[N + 1];
for (int i = 1; i <= N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
DP[1] = arr[1];
if (N >= 2) {
DP[2] = arr[1] + arr[2];
}
for (int i = 3; i <= N; i++) {
DP[i] = Math.max(DP[i - 2], DP[i - 3] + arr[i - 1]) + arr[i];
}
System.out.println(DP[N]);
}
}
댓글남기기