티스토리 뷰

https://www.acmicpc.net/problem/25556

 

25556번: 포스택

포닉스가 순열을 청소할 수 있으면 YES, 불가능하다면 NO를 출력한다.

www.acmicpc.net

class Node {
	constructor(item) {
		this.item = item;
		this.prev = null;
	}
}

class Stack {
	constructor() {
		this.top = null;
		this.size = 0;
	}

	empty() {
		return this.size === 0;
	}

	push(item) {
		const node = new Node(item);
		node.prev = this.top;
		this.top = node;
		this.size += 1;
	}

	pop() {
		const popItem = this.top;
		this.top = this.top.prev;
		this.size -= 1;
		return popItem.item;
	}
}

const [[N], nums] = require('fs')
	.readFileSync('./dev/stdin')
	.toString()
	.trim()
	.split('\n')
	.map((v) => v.split(' ').map(Number));
const stack1 = new Stack();
const stack2 = new Stack();
const stack3 = new Stack();
const stack4 = new Stack();

for (let i = 0; i < N; i++) {
	const target = nums[i];
	if (stack1.empty() || stack1.top.item < target) {
		stack1.push(target);
	} else if (stack2.empty() || stack2.top.item < target) {
		stack2.push(target);
	} else if (stack3.empty() || stack3.top.item < target) {
		stack3.push(target);
	} else if (stack4.empty() || stack4.top.item < target) {
		stack4.push(target);
	} else {
		console.log('NO');
		process.exit();
	}
}

console.log('YES');
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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 31
글 보관함