JS 코딩테스트 문제풀이(이중우선순위큐 )

Minji Lee·2023년 10월 22일
0

JS코딩테스트

목록 보기
18/122
post-thumbnail

이중우선순위 큐

이중우선순위 큐

문제

이중 우선순위 큐는 다음 연산을 할 수 있는 자료구조를 말합니다.

명령어수신 탑(높이)
I 숫자큐에 주어진 숫자를 삽입합니다.
D 1큐에서 최댓값을 삭제합니다.
D -1큐에서 최솟값을 삭제합니다.

이중 우선순위 큐가 할 연산 operations가 매개변수로 주어질 때, 모든 연산을 처리한 후 큐가 비어있으면 [0,0] 비어있지 않으면 [최댓값, 최솟값]을 return 하도록 solution 함수를 구현해주세요.

작성한 코드

function solution(operations) {
  let newQueue = [];
  operations.map((operation) => {
    operation = operation.split(" ");
    if (operation[0] === "I") {
      newQueue.push(Number(operation[1]));
    } else if (operation[0] === "D") {
      if (newQueue.length !== 0) {
        if (operation[1] === "1") {
          let max = Math.max(...newQueue);
          newQueue.splice(newQueue.indexOf(max), 1);
        } else if (operation[1] === "-1") {
          let min = Math.min(...newQueue);
          newQueue.splice(newQueue.indexOf(min), 1);
        }
      }
    }
  });
  if (newQueue.length === 0) {
    return [0, 0];
  } else {
    return [Math.max(...newQueue), Math.min(...newQueue)];
  }
}

풀이 및 해설

  1. operations 길이만큼 배열 반복
  2. 새로운 큐를 만들어서 I일 경우 큐에 넣기
  3. 만약 삭제할 때 큐가 비어있으면 continue → map 함수 사용하여 continue 불가
  4. 배열이 끝나면 큐에 값이 있는지 확인한 후 없으면 [0,0], 있으면 [최대값, 최소값] 반환

0개의 댓글