(int val)
| 12 | } |
| 13 | |
| 14 | public int add(int val) { |
| 15 | // add to heaq if it's less then k |
| 16 | if (q.size() < k) |
| 17 | q.offer(val); |
| 18 | else if (q.peek() < val) { |
| 19 | // if len(heaq) == k, and val greater than smallest num |
| 20 | // then pop smallest num than add val to heap |
| 21 | q.poll(); |
| 22 | q.offer(val); |
| 23 | } |
| 24 | return q.peek(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |