Algorithm/programmers

디펜스 게임 ( Level 2, JavaScript, 연습문제)

takeU 2025. 1. 14. 18:39
반응형
class MaxHeap {
  constructor() {
    this.heap = [];
  }

  getParentIndex(index) {
    return Math.floor((index - 1) / 2);
  }

  getLeftChildIndex(index) {
    return 2 * index + 1;
  }

  getRightChildIndex(index) {
    return 2 * index + 2;
  }

  swap(index1, index2) {
    [this.heap[index1], this.heap[index2]] = [this.heap[index2], this.heap[index1]];
  }

  insert(value) {
    this.heap.push(value);
    this.heapifyUp(this.heap.length - 1);
  }

  heapifyUp(index) {
    let currentIndex = index;
    let parentIndex = this.getParentIndex(currentIndex);

    while (currentIndex > 0 && this.heap[currentIndex] > this.heap[parentIndex]) {
      this.swap(currentIndex, parentIndex);
      currentIndex = parentIndex;
      parentIndex = this.getParentIndex(currentIndex);
    }
  }

  extractMax() {
    if (this.heap.length === 0) return null;

    const max = this.heap[0];
    const end = this.heap.pop();

    if (this.heap.length > 0) {
      this.heap[0] = end;
      this.heapifyDown(0);
    }

    return max;
  }

  heapifyDown(index) {
    let largest = index;
    const leftChildIndex = this.getLeftChildIndex(index);
    const rightChildIndex = this.getRightChildIndex(index);

    if (leftChildIndex < this.heap.length && this.heap[leftChildIndex] > this.heap[largest]) {
      largest = leftChildIndex;
    }

    if (rightChildIndex < this.heap.length && this.heap[rightChildIndex] > this.heap[largest]) {
      largest = rightChildIndex;
    }

    if (largest !== index) {
      this.swap(index, largest);
      this.heapifyDown(largest);
    }
  }

  peek() {
    return this.heap.length > 0 ? this.heap[0] : null;
  }

  size() {
    return this.heap.length;
  }
}


function solution(n, k, enemy) {
    let res = 0, i = 0
    const maxHeap = new MaxHeap();

    while (k >= 0 && n >= 0 && i < enemy.length) {
        n -= enemy[i]
        maxHeap.insert(enemy[i])
        if (n < 0 && k > 0) {
            const max = maxHeap.extractMax()
            n += max 
            k -= 1
        }
        res += 1
        i += 1
    }
    return n >= 0 ? res : res - 1
}

자바스크립트 최대힙 구현

n에서 일단 병사 제거 + 힙에 추가 한 뒤 n이 0 아래로 내려가면 heap에서 최대값을 더해주고 k를 소모하는 방식