1
var KthLargest = function (k, nums) {2
// sort ascending because I am familiar with it3
// when applying my binary search algorithm4
this.nums = nums.sort((a, b) => a - b);8
KthLargest.prototype.add = function (val) {9
// search a place to push val10
// using binary search12
right = this.nums.length - 1;13
while (left < right) {14
let mid = left + Math.floor((right + 1 - left) / 2);16
if (val < this.nums[mid]) {23
// push val into the nums24
this.nums.splice(left + (this.nums[left] < val ? 1 : 0), 0, val);26
// because our nums is sorted27
// so kth largest element is easy to be returned28
return this.nums[this.nums.length - this.k];