import java.util.*; public class Solution { private PriorityQueue<Integer> pq1; private PriorityQueue<Integer> pq2; public Solution() { this.pq1 = new PriorityQueue<>((o1, o2)->o2 - o1); this.pq2 = new PriorityQueue<>(); } public void Insert(Integer num) { if (!pq1.isEmpty() && num <= pq1.peek()) pq1.add(num); else pq2.add(num); int numToTrans = pq2.size() - pq1.size(); if (numToTrans == 2) { pq1.add(pq2.poll()); } numToTrans = pq1.size() - pq2.size(); if (numToTrans == 2) { pq2.add(pq1.poll()); } } public Double GetMedian() { if (pq1.size() == pq2.size()) return 1.0 * (pq1.peek() + pq2.peek()) / 2; else return pq1.size() > pq2.size() ? 1.0 * pq1.peek() : 1.0 * pq2.peek(); } }