From f1f2838580465078a7dfd0dfbd5b96ae448f1616 Mon Sep 17 00:00:00 2001 From: luckylooky2 Date: Sat, 30 Sep 2023 21:11:41 +0900 Subject: [PATCH] =?UTF-8?q?09.30=20:=20dp(=EA=B0=80=EC=9E=A5=20=EA=B8=B4?= =?UTF-8?q?=20=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=EC=88=98=EC=97=B4=204)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/14002.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 baekjoon/14002.js diff --git a/baekjoon/14002.js b/baekjoon/14002.js new file mode 100644 index 0000000..aeda655 --- /dev/null +++ b/baekjoon/14002.js @@ -0,0 +1,31 @@ +// 가장 긴 증가하는 부분 수열 4 : 동적 계획법 +const input = require("fs") + .readFileSync("/dev/stdin") + .toString() + .trim() + .split("\n") + .map((v) => v.split(" ").map((v) => parseInt(v, 10))); +const [n] = input.shift(); +const arr = input.shift(); +const dp = new Array(n + 1).fill([]); +let maxArr = []; + +for (let i = 1; i <= n; i++) { + const curr = arr[i - 1]; + const lst = []; + let maxLen = -Infinity; + let index = 0; + for (let j = 0; j < i; j++) { + if (dp[j].length === 0) lst.push([curr]); + else if (dp[j][dp[j].length - 1] < curr) lst.push(dp[j].concat([curr])); + } + for (let i = 0; i < lst.length; i++) { + maxLen = Math.max(lst[i].length, maxLen); + if (maxLen === lst[i].length) index = i; + } + dp[i] = lst[index]; + if (maxArr.length < dp[i].length) maxArr = dp[i]; +} + +console.log(maxArr.length); +console.log(maxArr.join(" "));