Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions live11/test115/문제1/조진우.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const input = require("fs")
.readFileSync(
process.platform === "linux"
? "/dev/stdin"
: require("path").join(__dirname, "input.txt"),
"utf8"
)
.trim()
.split("\n")
.map(Number);

function solution(input) {
const arr = input.slice(1);
let answer = 0;

function dfs(start, end) {
if (start > end) return null;
if (start === end) return arr[start];

let max = -Infinity;
let maxIdx = -1;
for (let i = start; i <= end; i++) {
if (arr[i] > max) {
max = arr[i];
maxIdx = i;
}
}

const leftMax = dfs(start, maxIdx - 1);
const rightMax = dfs(maxIdx + 1, end);

if (leftMax !== null) answer += max - leftMax;
if (rightMax !== null) answer += max - rightMax;

return max;
}

dfs(0, arr.length - 1);
return answer;
}

console.log(solution(input));
28 changes: 28 additions & 0 deletions live11/test115/문제2/조진우.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const input = require("fs")
.readFileSync(
process.platform === "linux"
? "/dev/stdin"
: require("path").join(__dirname, "input.txt"),
"utf8"
)
.trim()
.split("\n");

function solution(input) {
const N = +input[0];
const A = input[1].split(" ").map(Number);
const answer = new Array(N).fill(-1);
const stack = [];

for (let i = N - 1; i >= 0; i--) {
while (stack.length && stack[stack.length - 1] <= A[i]) stack.pop();

if (stack.length) answer[i] = stack[stack.length - 1];

stack.push(A[i]);
}

return answer.join(" ");
}

console.log(solution(input));