|
| 1 | +/** |
| 2 | + * @param {number} m |
| 3 | + * @param {number} k |
| 4 | + */ |
| 5 | +const MKAverage = function (m, k) { |
| 6 | + this.sum = 0 |
| 7 | + this.dataBuff = [] |
| 8 | + this.dataM = [] |
| 9 | + this.m = m |
| 10 | + this.k = k |
| 11 | + this.count = m - k - k |
| 12 | +} |
| 13 | +/** |
| 14 | + * @param {number} num |
| 15 | + * @return {void} |
| 16 | + */ |
| 17 | +MKAverage.prototype.addElement = function (num) { |
| 18 | + const total = this.dataBuff.length |
| 19 | + this.dataBuff.push(num) |
| 20 | + if (total >= this.m) { |
| 21 | + let index = binarySearch( |
| 22 | + this.dataBuff, |
| 23 | + this.dataM, |
| 24 | + this.dataBuff[total - this.m] |
| 25 | + ) |
| 26 | + this.dataM[index] = this.dataBuff.length - 1 |
| 27 | + if (index === 0 || num > this.dataBuff[this.dataM[index - 1]]) { |
| 28 | + move2End(this.dataBuff, this.dataM, index) |
| 29 | + } else if ( |
| 30 | + index === this.m - 1 || |
| 31 | + num < this.dataBuff[this.dataM[index - 1]] |
| 32 | + ) { |
| 33 | + move2Start(this.dataBuff, this.dataM, index) |
| 34 | + } |
| 35 | + |
| 36 | + this.sum = 0 |
| 37 | + } else { |
| 38 | + this.dataM.push(this.dataBuff.length - 1) |
| 39 | + move2Start(this.dataBuff, this.dataM, this.dataBuff.length - 1) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * @return {number} |
| 45 | + */ |
| 46 | +MKAverage.prototype.calculateMKAverage = function () { |
| 47 | + if (this.dataM.length < this.m) { |
| 48 | + return -1 |
| 49 | + } else { |
| 50 | + if (!this.sum) { |
| 51 | + this.sum = calcSum(this.dataBuff, this.dataM, this.k, this.count) |
| 52 | + } |
| 53 | + return Math.floor(this.sum / this.count) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Your MKAverage object will be instantiated and called as such: |
| 59 | + * var obj = new MKAverage(m, k) |
| 60 | + * obj.addElement(num) |
| 61 | + * var param_2 = obj.calculateMKAverage() |
| 62 | + */ |
| 63 | +function binarySearch(numArr, indexArr, tar) { |
| 64 | + let left = 0 |
| 65 | + let right = indexArr.length - 1 |
| 66 | + |
| 67 | + while (left <= right) { |
| 68 | + let mid = (left + right) >>> 1 |
| 69 | + |
| 70 | + if (numArr[indexArr[mid]] > tar) { |
| 71 | + right = mid - 1 |
| 72 | + } else if (numArr[indexArr[mid]] < tar) { |
| 73 | + left = mid + 1 |
| 74 | + } else { |
| 75 | + return mid |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +function move2Start(numArr, indexArr, index) { |
| 80 | + let tmp |
| 81 | + |
| 82 | + while (index > 0 && numArr[indexArr[index]] < numArr[indexArr[index - 1]]) { |
| 83 | + tmp = indexArr[index] |
| 84 | + indexArr[index] = indexArr[index - 1] |
| 85 | + indexArr[index - 1] = tmp |
| 86 | + index-- |
| 87 | + } |
| 88 | +} |
| 89 | +function move2End(numArr, indexArr, index) { |
| 90 | + let tmp |
| 91 | + |
| 92 | + while ( |
| 93 | + index < indexArr.length - 1 && |
| 94 | + numArr[indexArr[index]] > numArr[indexArr[index + 1]] |
| 95 | + ) { |
| 96 | + tmp = indexArr[index] |
| 97 | + indexArr[index] = indexArr[index + 1] |
| 98 | + indexArr[index + 1] = tmp |
| 99 | + index++ |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function calcSum(numArr, indexArr, start, count) { |
| 104 | + let sum = 0 |
| 105 | + for (let i = 0; i < count; i++) { |
| 106 | + sum += numArr[indexArr[i + start]] |
| 107 | + } |
| 108 | + return sum |
| 109 | +} |
0 commit comments