티스토리 뷰
https://www.acmicpc.net/problem/1374
1374번: 강의실
첫째 줄에 강의의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 줄마다 세 개의 정수가 주어지는데, 순서대로 강의 번호, 강의 시작 시간, 강의 종료 시간을 의미한다. 강의
www.acmicpc.net
같은 문제
https://www.acmicpc.net/problem/19598
19598번: 최소 회의실 개수
서준이는 아빠로부터 N개의 회의를 모두 진행할 수 있는 최소 회의실 개수를 구하라는 미션을 받았다. 각 회의는 시작 시간과 끝나는 시간이 주어지고 한 회의실에서 동시에 두 개 이상의 회의
www.acmicpc.net
https://www.acmicpc.net/problem/11000
11000번: 강의실 배정
첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)
www.acmicpc.net
const [N, ...arr] = require('fs')
.readFileSync('./dev/stdin')
.toString()
.trim()
.split('\n')
.map((v) => v.split(' ').map(Number));
class MinHeap {
constructor() {
this.heap = [];
}
swap(a, b) {
[this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]];
}
size() {
return this.heap.length;
}
push(value) {
this.heap.push(value);
let current = this.heap.length - 1;
let parent = Math.floor((current - 1) / 2);
while (this.heap[parent] > value) {
this.swap(parent, current);
current = parent;
parent = Math.floor((current - 1) / 2);
}
}
pop() {
const last = this.heap.length - 1;
let current = 0;
this.swap(current, last); // 0번이 루트노드
const value = this.heap.pop();
while (current < last) {
let left = current * 2 + 1;
let right = current * 2 + 2;
if (left >= last) {
break;
} else if (right >= last) {
if (this.heap[current] > this.heap[left]) {
this.swap(current, left);
current = left;
} else {
break;
}
} else {
if (this.heap[left] < this.heap[current] || this.heap[right] < this.heap[current]) {
let next = this.heap[left] < this.heap[right] ? left : right;
this.swap(current, next);
current = next;
} else {
break;
}
}
}
return value;
}
}
const minHeap = new MinHeap();
const sorted = arr.sort((a, b) => {
return a[1] - b[1];
});
for (let i = 0; i < sorted.length; i++) {
const [_, s, t] = sorted[i];
if (minHeap.size() == 0) {
minHeap.push(t);
} else {
const head = minHeap.heap[0];
if (s >= head) {
minHeap.pop();
}
minHeap.push(t);
}
}
console.log(minHeap.size());
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 17352번: 여러분의 다리가 되어 드리겠습니다! (0) | 2023.09.01 |
---|---|
Node.js) 백준 2800번: 괄호 제거 (0) | 2023.09.01 |
Node.js)백준 19598번: 최소 회의실 개수 (0) | 2023.08.31 |
Node.js) 백준 1662번: 압축 (0) | 2023.08.31 |
Node.js) 백준 1351번: 무한 수 (0) | 2023.08.31 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 서버개발
- 다이나밍프로그래밍
- 최소공통조상
- 면접비
- 개발자면접
- node.js
- 그래프
- 다이나믹프로그래밍
- 투포인터 연습
- create db
- 동적프로그래밍
- 롱베케이션
- 투포인터
- 로드나인
- MySQL
- 서버점검
- create databases;
- 면접질문
- BFS
- DB 생성
- 은둔청년체험
- MOD
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함