Title: #1825 - Finding MK Average - LeetCode JavaScript Solutions
Open Graph Title: #1825 - Finding MK Average - LeetCode JavaScript Solutions
X Title: #1825 - Finding MK Average - LeetCode JavaScript Solutions
Description: You are given two integers, m and k, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage fo...
Open Graph Description: You are given two integers, m and k, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage fo...
X Description: You are given two integers, m and k, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage fo...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/finding-mk-average
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/finding-mk-average","name":"#1825 - Finding MK Average - LeetCode JavaScript Solutions","description":"You are given two integers, m and k, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage fo...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} m\n * @param {number} k\n */\nvar MKAverage = function(m, k) {\n this.m = m;\n this.k = k;\n this.stream = [];\n this.sortedStream = [];\n this.sum = 0;\n};\n\n/**\n * @param {number} num\n * @return {void}\n */\nMKAverage.prototype.addElement = function(num) {\n this.stream.push(num);\n\n const insertIndex = this.findInsertPosition(num);\n this.sortedStream.splice(insertIndex, 0, num);\n this.sum += num;\n\n if (this.stream.length > this.m) {\n const oldestElement = this.stream.shift();\n const oldestIndex = this.sortedStream.indexOf(oldestElement);\n this.sortedStream.splice(oldestIndex, 1);\n this.sum -= oldestElement;\n }\n};\n\n/**\n * @return {number}\n */\nMKAverage.prototype.calculateMKAverage = function() {\n if (this.stream.length < this.m) {\n return -1;\n }\n\n let adjustedSum = this.sum;\n\n for (let i = 0; i < this.k; i++) {\n adjustedSum -= this.sortedStream[i];\n }\n for (let i = this.sortedStream.length - this.k; i < this.sortedStream.length; i++) {\n adjustedSum -= this.sortedStream[i];\n }\n\n return Math.floor(adjustedSum / (this.m - 2 * this.k));\n};\n\n/**\n * Helper method to find insert position using binary search\n * @param {number} num\n * @return {number}\n */\nMKAverage.prototype.findInsertPosition = function(num) {\n let left = 0;\n let right = this.sortedStream.length - 1;\n\n while (left <= right) {\n const mid = Math.floor((left + right) / 2);\n if (this.sortedStream[mid] < num) {\n left = mid + 1;\n } else {\n right = mid - 1;\n }\n }\n\n return left;\n};","keywords":"LeetCode 1825, #1825 - Finding MK Average, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/finding-mk-average |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/finding-mk-average/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1825-finding-mk-average.js |
| Heap Priority Queue | https://leetcodejavascript.com/tags/heap-priority-queue |
| Design | https://leetcodejavascript.com/tags/design |
| Ordered Set | https://leetcodejavascript.com/tags/ordered-set |
| Queue | https://leetcodejavascript.com/tags/queue |
| Data Stream | https://leetcodejavascript.com/tags/data-stream |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow