Skip to content

Commit eb500c9

Browse files
committed
[boj] 옥상 정원 꾸미기 [G5]
1 parent 1548a3c commit eb500c9

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

허현빈/4주차/260123-1.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const input = require("fs")
2+
.readFileSync("./dev/stdin.txt")
3+
.toString()
4+
.trim()
5+
.split("\n")
6+
.map((e) => +e);
7+
8+
const ans = () => {
9+
const n = input.shift();
10+
const arr = input;
11+
let result = 0;
12+
const stack = []; // 높이만 저장
13+
14+
for (let i = arr.length - 1; i >= 0; i--) {
15+
const height = arr[i];
16+
17+
// 현재 높이보다 낮은 건물들을 모두 제거
18+
while (stack.length && stack[stack.length - 1] < height) {
19+
stack.pop();
20+
}
21+
22+
// 스택에 남은 건물 수 = 현재 건물이 볼 수 있는 건물 수
23+
result += stack.length;
24+
stack.push(height);
25+
}
26+
27+
console.log(result);
28+
};
29+
ans();

허현빈/4주차/260123-2.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const input = require("fs")
2+
.readFileSync("./dev/stdin.txt")
3+
.toString()
4+
.trim()
5+
.split("\n")
6+
.map((e) => e.split(" "))
7+
.map((e) => e.map((e2) => Number(e2)));
8+
9+
input.pop();
10+
11+
const ans = () => {
12+
const ans = [];
13+
const arr = input;
14+
for (let i = 0; i < arr.length; i++) {
15+
arr[i].shift();
16+
ans.push(findAns(arr[i]));
17+
}
18+
19+
function findAns(arr) {
20+
const lStack = [];
21+
const lAns = [];
22+
const rStack = [];
23+
const rAns = [];
24+
const tempAns = [];
25+
26+
for (let i = 0; i < arr.length; i++) {
27+
const arrVal = arr[i];
28+
let count = 0;
29+
if (lStack.length) {
30+
const val = lStack[lStack.length - 1];
31+
if (val >= arrVal) {
32+
lStack.pop();
33+
count += arrVal;
34+
}
35+
lStack.push(arrVal);
36+
}
37+
lAns.push(arrVal);
38+
}
39+
40+
console.log(lAns);
41+
for (let i = arr.length - 1; i >= 0; i--) {}
42+
for (let i = 0; i < arr.length; i++) {
43+
tempAns[i] + arr[i];
44+
}
45+
46+
return Math.max(...tempAns);
47+
}
48+
};
49+
ans();

0 commit comments

Comments
 (0)