function solution(operations) {
var answer = [];
function Queue(){
this.q = [];
this.insert = function(num){
this.q.push(num);
};
this.deleteElement = function(idx){
this.q.splice(idx, 1);
}
this.deleteMax = function(){
if(this.q.length > 0){
const MAX = Math.max(...this.q);
const INDEX = this.q.indexOf(MAX);
this.deleteElement(INDEX);
}
};
this.deleteMin = function(){
if(this.q.length > 0){
const MAX = Math.min(...this.q);
const INDEX = this.q.indexOf(MAX);
this.deleteElement(INDEX);
}
}
this.getQ = function(){
return this.q;
}
this.getMinMax = function(){
if(this.q.length < 1) return [0, 0];
return [Math.max(...this.q), Math.min(...this.q)];
}
}
const q = new Queue();
operations.map(oper => {
const [command, num] = oper.split(' ');
if(command === 'I'){
q.insert(+num);
}
if(command === 'D' && num === '1'){
q.deleteMax();
}
if(command === 'D' && num === '-1'){
q.deleteMin();
}
})
return q.getMinMax();
}
'코딩테스트 문제풀이' 카테고리의 다른 글
[프로그래머스, javascript] 네트워크 (0) | 2021.05.11 |
---|---|
[구름, javascript] 배열 합치기 (0) | 2021.04.17 |
[프로그래머스, javascript] 타겟 넘버 DFS와 BFS 풀이 (0) | 2021.04.06 |
[프로그래머스, javascript] 가장 긴 팰린드롬 (0) | 2021.03.28 |
[프로그래머스, javascript] 여행 경로 (0) | 2021.03.26 |