프로그래머스 LEVEL 3(이중우선순위큐)
-
사용 언어 : javascript
-
해결 날짜 : 2022-09-27
- 해결 방법 :
- Insert의 경우 heap에 push() 후 내림차순 정렬
- Delete의 경우 1이면 가장 큰 값, -1이면 가장 작은 값 pop()
- 회고 :
- x
-
코드
function solution(operations) { const heap = []; for (const operation of operations) { const splitted_operation = operation.split(' '); const [command, number] = [splitted_operation[0], Number(splitted_operation[1])]; if (command === 'I') { heap.push(number); heap.sort((a, b) => b - a); } else { if (!heap.length) continue; number === 1 ? heap.shift() : heap.pop(); } } return heap.length ? [heap[0], heap[heap.length - 1]] : [0, 0]; }
- 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges