[백준 14002번] DP(다이나믹 프로그래밍) - 가장 긴 증가하는 부분 수열 4

김민지·2023년 8월 30일
0

냅다 시작 백준

목록 보기
83/118

✨ 문제 ✨

✨ 정답 ✨

const { count } = require("console");
const fs = require("fs");
const { nextTick } = require("process");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
let input = fs.readFileSync(filePath).toString().trim();


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

input=input.split('\n');
let N=+input.shift();

let array=input.shift().split(' ').map((el)=>+el);
let dp=new Array(N).fill(0)
let dpArray=[];

for (let i=0;i<N;i++){
    let max=0;
    let maxIndex=-1;
    for (let j=0;j<i;j++){
        if (array[i]>array[j]&&dp[j]>max){
            max=dp[j];
            maxIndex=j;
        }
    }
    dp[i]=max+1;
    if (maxIndex===-1){
        dpArray[i]=[array[i]]
    }else{
        dpArray[i]=[...dpArray[maxIndex], array[i]]
    }
}

console.log(Math.max(...dp))
console.log(dpArray[dp.indexOf(Math.max(...dp))].join(' '))

🧵 참고한 정답지 🧵

💡💡 기억해야 할 점 💡💡

profile
이건 대체 어떻게 만든 거지?

0개의 댓글