티스토리 뷰
https://www.acmicpc.net/problem/1725
// https://www.acmicpc.net/problem/1725
// 히스토그램
class Item {
prev: null | Item = null;
constructor(public value: any) {}
}
class Stack {
top: null | Item = null;
size = 0;
constructor() {}
push(value: any) {
const node = new Item(value);
node.prev = this.top;
this.top = node;
this.size += 1;
}
pop() {
if (this.top) {
const popItem = this.top;
this.top = this.top.prev;
this.size -= 1;
return popItem.value;
} else return null;
}
peek() {
return this.top ? this.top.value : null;
}
isEmpty() {
return this.size == 0;
}
}
const input: string[] = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n');
input.shift();
let answer = 0;
const st = new Stack();
input.forEach((num: any) => {
num = Number(num);
if (st.isEmpty()) {
if (num > answer) answer = num;
st.push({ h: num, w: 1 });
} else {
if (num > answer) answer = num;
let sz = 0;
while (!st.isEmpty() && st.peek().h > num) {
sz = sz == 0 ? st.peek().w : sz + st.peek().w;
const possible = st.pop().h * sz;
if (possible > answer) answer = possible;
}
if (st.isEmpty() || st.peek().h < num) {
st.push({ h: num, w: sz + 1 });
} else if (st.peek().h == num) {
st.peek().w += sz + 1;
}
}
});
let sz = 0;
while (!st.isEmpty()) {
sz = sz == 0 ? st.peek().w : sz + st.peek().w;
const max = st.pop().h * sz;
if (max > answer) answer = max;
}
console.log(answer);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
TS) 백준 10090번: Counting Inversions (0) | 2024.03.04 |
---|---|
TS) 백준 1019번: 책 페이지 (0) | 2024.03.03 |
TS) 백준 15964번: 이상한 기호 (0) | 2024.03.03 |
TS) 백준 6549번: 히스토그램에서 가장 큰 직사각형 (0) | 2024.03.03 |
TS) 백준 7662번: 이중 우선순위 큐 (0) | 2024.03.01 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BFS
- create db
- 서버개발
- 다이나믹프로그래밍
- MOD
- 투포인터 연습
- 서버점검
- 최소공통조상
- 면접비
- DB 생성
- node.js
- 롱베케이션
- create databases;
- 다이나밍프로그래밍
- 동적프로그래밍
- 투포인터
- MySQL
- 개발자면접
- 면접질문
- 그래프
- 은둔청년체험
- 로드나인
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
글 보관함