diff --git a/code_study/Baekjoon/java/_2579.java b/code_study/Baekjoon/java/_2579.java new file mode 100644 index 0000000..5d909a3 --- /dev/null +++ b/code_study/Baekjoon/java/_2579.java @@ -0,0 +1,40 @@ +import java.util.*; + +public class _2579{ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + int[] stair = new int[N+1]; + for(int i=1; i<=N; i++){ + stair[i] = sc.nextInt(); + } + + int[] dp = new int[N+1]; + + switch (N) { + case 1: + System.out.println(stair[1]); + break; + + case 2: + System.out.println(stair[1]+stair[2]); + break; + + case 3: + System.out.println(Math.max(stair[1] + stair[3], stair[2] + stair[3])); + break; + + default: + dp[1] = stair[1]; + dp[2] = stair[1] + stair[2]; + dp[3] = Math.max(stair[1] + stair[3], stair[2] + stair[3]); + for(int i=4; i<=N; i++){ + dp[i] = Math.max(dp[i-2] + stair[i], dp[i-3] + stair[i-1] + stair[i]); + } + System.out.println(dp[N]); + break; + } + + sc.close(); + } +} \ No newline at end of file diff --git a/code_study/Baekjoon/python/1463.py b/code_study/Baekjoon/python/1463.py new file mode 100644 index 0000000..54f1157 --- /dev/null +++ b/code_study/Baekjoon/python/1463.py @@ -0,0 +1,10 @@ +maxValue = 10**6 +N = int(input()) +dp = [0]*(N+1) +for i in range(2,N+1): + dp[i] = min( + dp[i-1]+1, + dp[i//2]+1 if i%2==0 else maxValue, + dp[i//3]+1 if i%3==0 else maxValue + ) +print(dp[N]) \ No newline at end of file diff --git a/code_study/Baekjoon/ts/2579.ts b/code_study/Baekjoon/ts/2579.ts new file mode 100644 index 0000000..8851848 --- /dev/null +++ b/code_study/Baekjoon/ts/2579.ts @@ -0,0 +1,19 @@ +export {}; + +const input: number[] = require("fs").readFileSync(0,"utf8").toString().trim().split('\n').map(Number); +const N:number = input[0]; +if(N<=3){ + console.log(N===1 ? input[1] : N===2 ? input[1]+input[2] : Math.max(input[1]+input[3], input[2]+input[3])); +} +else{ + let score: number[] = new Array(N+1).fill(0); + score[1] = input[1]; + score[2] = input[1]+input[2]; + score[3] = Math.max(input[1]+input[3], input[2]+input[3]); + score.forEach((_,i,arr) => { + if(i>3){ + arr[i] = Math.max(arr[i-2]+input[i], arr[i-3] + input[i-1] + input[i]); + } + }); + console.log(score[N]); +} \ No newline at end of file