import java.util.Scanner; /* * 2579번 계단 오르기 */ public class Main { static int[] x; static int[] memo; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); x = new int[n + 1]; memo = new int[n + 1]; for (int i = 1; i <= n; i++) { x[i] = sc.nextInt(); } System.out.println(f(n, 2)); sc.close(); } private static int f(int n, int t) { if (n == 0) return x[0]; if (n == 1) { if (t == 1) { return x[1]; } else { return x[1] + x[0]; } } if (t == 1) { if(memo[n] > 0) return memo[n]; return memo[n] = x[n] + f(n - 2, 2); } else { return x[n] + Math.max(f(n - 1, 1), f(n - 2, 2)); } } }
[백준 알고리즘 풀이] 2579번 계단 오르기
전형적인 동적 계획법으로 recursive와 memoization 기법을 활용했다. 한 계단 또는 두 계단이기 때문에 n - 1, n - 2 모든 경우의 트리에서 최대값을 출력하도록 구현한다.
Subscribe to:
Post Comments (Atom)
-
Opening the black box of Deep Neural Networks via Information - https://arxiv.org/pdf/1703.00810.pdf 지금까지 딥 러닝은 어떻게 동작하는지 이해할 수 없다고 믿어져왔다...
-
음성 인공지능 분야에서 스타트업이 생각해볼 수 있는 전략은 아마 다음과 같이 3가지 정도가 있을 것이다: 독자적 Vertical 음성 인공지능 Application 구축 기 음성 플랫폼을 활용한 B2B2C 형태의 비지니스 구축 기 음성 플랫폼...
-
As mentioned ago, I've been forming up the Hamburg project with Hyunsik Choi. Let's see more detail in the diagram of computing met...
No comments:
Post a Comment