题解 | 售价的中位数
售价的中位数
https://www.nowcoder.com/practice/a1c7e3a0b2fa46da8a3430617b7d74ca
import java.util.*; public class Solution { public double[] findMedianPrice (int[] prices) { PriorityQueue<Integer> l = new PriorityQueue<>((o1, o2) -> o2 - o1); PriorityQueue<Integer> r = new PriorityQueue<>(); final int n = prices.length; double[] ans = new double[n]; for (int i = 0; i < n; ++i) { l.add(prices[i]); if(!r.isEmpty() && l.peek() > r.peek()) { final int t1 = l.poll(); final int t2 = r.poll(); l.add(t2); r.add(t1); } if (l.size() - r.size() == 2) { r.add(l.poll()); } if ((i & 1) == 1) { ans[i]= 0.5d * (l.peek() + r.peek()); } else { ans[i] = 1d * l.peek(); } } return ans; } }