본문 바로가기

백준

[백준] 2579 계단 오르기 [자바]

 


https://st-lab.tistory.com/132

 

[백준] 2579번 : 계단 오르기 - JAVA [자바]

www.acmicpc.net/problem/2579 과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점" data-og-host="www.acmicpc.net" data-og-source-url="https://www.acmicpc.net/proble..

st-lab.tistory.com

 


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
	static int[] arr;
	static int[] stair = null;
	public static void main(String arg[]) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	int N = Integer.parseInt(br.readLine());
    	stair = new int[N+1];
    	arr = new int[N+1];
    	for(int i = 1; i <= N; i++)
    		arr[i] = Integer.parseInt(br.readLine());
    	stair[1] = arr[0];
    	stair[0] = 0;
    	dp(N);
    	System.out.println(stair[N]);
    }
	static int dp(int n) {
		if(n <= 0)
			return 0;
		if(stair[n] == 0) {
			stair[n] = Math.max(dp(n-2), dp(n-3)+arr[n-1]) + arr[n];
			return stair[n];
		}
		else
			return stair[n];
	}
}

'백준' 카테고리의 다른 글

[백준] 2206 벽 부수고 이동하기 [자바]  (0) 2021.12.07
[백준] 7576 토마토 [자바]  (0) 2021.12.07
[백준] 11050 이항 계수 1 [자바]  (0) 2021.11.29
[백준] 10814 나이순 정렬  (0) 2021.11.28
[백준] 2178 미로 탐색  (0) 2021.11.28