-
[2579] 계단 오르기 - Java알고리즘 연습 2022. 3. 23. 15:38
1. 문제
https://www.acmicpc.net/problem/2579
2579번: 계단 오르기
계단 오르기 게임은 계단 아래 시작점부터 계단 꼭대기에 위치한 도착점까지 가는 게임이다. <그림 1>과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점
www.acmicpc.net
2. 풀이
import java.io.*; public class Boj2579 { public static void main(String[] args) throws IOException { // 1. Read the stairs BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int height = Integer.parseInt(br.readLine()); // 2. Allocate values to array int[] stairs = new int[301]; int[] dp = new int[301]; // the stair starts from index 1 for (int i = 1; i <= height; i++) { stairs[i] = Integer.parseInt(br.readLine()); } // 3. Calculate the maximum score with DP dp[1] = stairs[1]; dp[2] = dp[1] + stairs[2]; // case 1: jump from the last step // (in this case, they should have jumped 2 steps at once to get there) // case 2: jump from the 2nd last step for (int i = 3; i <= height; i++) { dp[i] = Math.max((dp[i - 3] + stairs[i - 1]), dp[i - 2]) + stairs[i]; } // 4. Print the maximum score System.out.println(dp[height]); } }
3. 배운점
4. 개선할 점
풀이 오류 지적, 다른 접근법 공유, 그 밖에 질문 등 모든 의견을 환영합니다.
'알고리즘 연습' 카테고리의 다른 글
[4396] 지뢰 찾기 - Python (0) 2022.06.13 [1439] 뒤집기 - Python (0) 2022.04.07 [1003번] 피보나치 함수 - Java (0) 2022.03.18 [11057] 오르막수 - Java (0) 2022.03.15 [11052] 카드 구매하기 - Java (0) 2022.03.14