슈콩

[BOJ] 백준 2579 계단 오르기 본문

Algorithms/Baekjoon

[BOJ] 백준 2579 계단 오르기

shukong 2025. 8. 26. 15:49
계단을 연속으로 3칸 밟을 수 없다(제약조건) : 1,2 연속으로 몇 계단을 밟았는지 정보를 Check!

 

[문제]

https://www.acmicpc.net/problem/2579

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	int n = Integer.parseInt(br.readLine());
    	int[] stair = new int[n+1];
    	int[][] dp = new int[n+1][3];
    	for(int i=1;i<=n;i++) {
    		stair[i] = Integer.parseInt(br.readLine());
    	}
    	dp[1][1] = stair[1];
    	for(int i=2;i<=n;i++) {
    		dp[i][1] = Math.max(dp[i-2][1],dp[i-2][2]) + stair[i];
    		dp[i][2] = dp[i-1][1] + stair[i];
    	}
    	System.out.println(Math.max(dp[n][1], dp[n][2]));
    }
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 4883 삼각 그래프  (0) 2025.08.26
[BOJ] 백준 2748 피보나치 수 2  (0) 2025.08.26
[BOJ] 백준 2482 색상환  (0) 2025.08.26
[BOJ] 백준 2302 극장 좌석  (2) 2025.08.26
[BOJ] 백준 2294 동전 2  (0) 2025.08.26