using System.Collections.Generic;
public MaxHeap(int capacity)
this.capacity = capacity;
heap = new List<int>(capacity);
for(int i = 0; i < heap.Count; i++)
public void Insert(int score)
if (heap.Count >= capacity)
int currentIndex = heap.Count - 1;
int parentIndex = (currentIndex - 1) / 2;
if (heap[currentIndex] <= heap[parentIndex])
int temp = heap[currentIndex];
heap[currentIndex] = heap[parentIndex];
heap[parentIndex] = temp;
currentIndex = parentIndex;
public int GetHighScore()
if (heap.Count == 0) return 0;
heap[0] = heap[heap.Count - 1];
heap.RemoveAt(heap.Count - 1);
int leftChild = currentIndex * 2 + 1;
int rightChild = currentIndex * 2 + 2;
int largest = currentIndex;
if (leftChild < heap.Count && heap[leftChild] > heap[largest])
if (rightChild < heap.Count && heap[rightChild] > heap[largest])
if (largest == currentIndex)
int temp = heap[currentIndex];
heap[currentIndex] = heap[largest];