https://programmers.co.kr/learn/courses/30/lessons/42628
function solution(operations) {
const queue = [];
const operationArr = operations.map((item) => item.split(' '));
for(const [op, value] of operationArr) {
if (op === 'I') {
queue.push(+value);
queue.sort((a,b) => +a - +b);
continue;
}
if (value === '1') {
queue.pop();
} else if (value === '-1') {
queue.shift();
}
}
if (queue.length === 0) return [0, 0];
return [queue[queue.length-1], queue[0]];
}
큐에 삽입이 될 때마다 정렬을 하는 방식으로 했는데 시간 초과가 되지 않았다. 이렇게 정렬을 하지 않고 풀 수 있는 방법이 있을까?