Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions KHJ_Root/백준문제/BackJoon2579.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int n = sc.nextInt(); // 계단 개수
int[] score = new int[n + 1];
int[] dp = new int[n + 1];

for (int i = 1; i <= n; i++) {
score[i] = sc.nextInt();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중요한건 아니지만 BufferedReader를 사용하면 조금 더 빠릅니다!

}

// 초기값 설정
dp[1] = score[1];
if (n >= 2) dp[2] = score[1] + score[2];
if (n >= 3) dp[3] = Math.max(score[1] + score[3], score[2] + score[3]);

// 점화식 적용
for (int i = 4; i <= n; i++) {
dp[i] = Math.max(dp[i-2], dp[i-3] + score[i-1]) + score[i];
}

System.out.println(dp[n]);
}
}