Title: #1675 - Minimize Deviation in Array - LeetCode JavaScript Solutions
Open Graph Title: #1675 - Minimize Deviation in Array - LeetCode JavaScript Solutions
X Title: #1675 - Minimize Deviation in Array - LeetCode JavaScript Solutions
Description: You are given an array nums of n positive integers. You can perform two types of operations on any element of the array any number of times:...
Open Graph Description: You are given an array nums of n positive integers. You can perform two types of operations on any element of the array any number of times:...
X Description: You are given an array nums of n positive integers. You can perform two types of operations on any element of the array any number of times:...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/minimize-deviation-in-array
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/minimize-deviation-in-array","name":"#1675 - Minimize Deviation in Array - LeetCode JavaScript Solutions","description":"You are given an array nums of n positive integers. You can perform two types of operations on any element of the array any number of times:...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minimumDeviation = function(nums) {\n const heap = [];\n let minElement = Infinity;\n\n for (const num of nums) {\n const value = num % 2 ? num * 2 : num;\n heap.push(value);\n minElement = Math.min(minElement, value);\n }\n\n for (let i = Math.floor(heap.length / 2) - 1; i >= 0; i--) {\n siftDown(i);\n }\n\n let minDeviation = heap[0] - minElement;\n\n while (heap[0] % 2 === 0) {\n const maxElement = heap[0];\n heap[0] = maxElement / 2;\n minElement = Math.min(minElement, heap[0]);\n siftDown(0);\n minDeviation = Math.min(minDeviation, heap[0] - minElement);\n }\n\n return minDeviation;\n\n function siftDown(index) {\n while (2 * index + 1 < heap.length) {\n let maxChild = 2 * index + 1;\n if (maxChild + 1 < heap.length && heap[maxChild + 1] > heap[maxChild]) {\n maxChild++;\n }\n if (heap[index] < heap[maxChild]) {\n [heap[index], heap[maxChild]] = [heap[maxChild], heap[index]];\n index = maxChild;\n } else {\n break;\n }\n }\n }\n};","keywords":"LeetCode 1675, #1675 - Minimize Deviation in Array, 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/minimize-deviation-in-array |
| 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/minimize-deviation-in-array/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1675-minimize-deviation-in-array.js |
| Array | https://leetcodejavascript.com/tags/array |
| Greedy | https://leetcodejavascript.com/tags/greedy |
| Heap Priority Queue | https://leetcodejavascript.com/tags/heap-priority-queue |
| Ordered Set | https://leetcodejavascript.com/tags/ordered-set |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow