[백준 1912] 다이나믹 프로그래밍 - 연속합

김민지·2023년 3월 26일
0

냅다 시작 백준

목록 보기
35/118

✨ 문제 ✨

✨ 정답 ✨

const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
let input = fs.readFileSync(filePath).toString().trim().split("\n");

// const fs = require('fs'); 
// let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');


let N = +input[0]

input = input[1].split(' ')
input = input.map((el) => +el)

// 습관적으로 .shift()를 해서 틀렸었다.

let dp = new Array(N).fill(0);
dp[0] = input[0]

if (N >= 2) {
    for (let i = 1; i < N; i++) {
        dp[i] = Math.max(input[i] + dp[i - 1], input[i] + dp[i])
    }
}

console.log(Math.max(...dp))
profile
이건 대체 어떻게 만든 거지?

0개의 댓글