Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 타겟 넘버
- 이중우선순위큐
- react-native
- 전화번호 목록
- 완주하지 못한 선수
- Programmers
- 기능개발
- react-native bind
- 더 맵게
- 디스크 컨트롤러
- Stack
- 깊이우선탐색
- Virtual DOM
- 가장 큰 수
- Algorithm
- 넓이우선탐색
- react
- Queue
- hash
- 소수찾기
- 주식
- heap
- 다리를 지나는 트럭
- k번째수
- Data Structure
- browser workflow
- react-native-navigation
- Brute Force
- Javascript
- sorting
Archives
- Today
- Total
개발 블로그
[프로그래머스/Javascript/Heap] 이중우선순위큐 본문
1. 서론
위 문제는 Heap으로 분류되어 있으나, javascript 에서는 Heap에 대한 라이브러리가 존재하지 않기때문에 직접 구현해야한다. heap에 대한 코드는 이전에 작성한 jun0127.tistory.com/11 에 있는 코드를 그대로 사용하였다.
2. 문제설명(출처: programmers.co.kr/learn/courses/30/lessons/42628)
3. 문제 풀이
1) operations의 명령어에 따라 I가 나오면 우선순위 큐에 해당 데이터 값을 enqueue한다.
2) 사전에 구현된 heap 코드는 min heap이기 때문에 D 1 명령어가 나오면 min heap을 max heap으로 변경하고 다시 min heap상태로 되돌린다.
3) D -1 명령어가 나오면 heap이 항상 min heap 유지하기 때문에 일반적인 remove 연산을 한다.
4) 마지막으로 min heap일 때, root 값을 구해 최솟값을 얻고, max heap으로 변경한 뒤 root 값을 구해 최댓값을 획득한다.
4. 결과코드
function solution(operations) {
var answer = [];
var h = new Heap();
for(let operation of operations){
let oper = operation.split(' ')
if(oper[0] == 'I'){
h.insert(Number(oper[1]), Number(oper[1]))
}else{
if(oper[1] == '-1'){
h.remove()
}else{
h.convertMaxHeap()
h.remove()
h.convertMinHeap()
}
}
}
if(!h.isEmpty()){
h.convertMaxHeap()
answer.push(h.peek().value)
h.convertMinHeap()
answer.push(h.peek().value)
}else{
answer = [0, 0]
}
return answer;
}
'IT > Programmers' 카테고리의 다른 글
[프로그래머스/Javascript/Stack+Queue] 기능개발 (0) | 2021.04.06 |
---|---|
[프로그래머스/Javascript/Stack] 주식가격 (0) | 2021.04.06 |
[프로그래머스/Javascript/Queue] 다리를 지나는 트럭 (0) | 2021.04.05 |
[프로그래머스/python/Heap] 더 맵게 (0) | 2021.04.05 |
[프로그래머스/Javascript/Heap] 디스크 컨트롤러 (0) | 2021.04.04 |