20250714 baekjoon

This commit is contained in:
songyc macbook 2025-07-14 16:40:26 +09:00
parent bb32d3b093
commit 6796fb6dc4
3 changed files with 69 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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])

View File

@ -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]);
}