Title: #1649 - Create Sorted Array through Instructions - LeetCode JavaScript Solutions
Open Graph Title: #1649 - Create Sorted Array through Instructions - LeetCode JavaScript Solutions
X Title: #1649 - Create Sorted Array through Instructions - LeetCode JavaScript Solutions
Description: Given an integer array instructions, you are asked to create a sorted array from the elements in instructions. You start with an empty conta...
Open Graph Description: Given an integer array instructions, you are asked to create a sorted array from the elements in instructions. You start with an empty conta...
X Description: Given an integer array instructions, you are asked to create a sorted array from the elements in instructions. You start with an empty conta...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/create-sorted-array-through-instructions
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/create-sorted-array-through-instructions","name":"#1649 - Create Sorted Array through Instructions - LeetCode JavaScript Solutions","description":"Given an integer array instructions, you are asked to create a sorted array from the elements in instructions. You start with an empty conta...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} instructions\n * @return {number}\n */\nvar createSortedArray = function(instructions) {\n const MOD = 1e9 + 7;\n const maxValue = Math.max(...instructions);\n const fenwickTree = new Array(maxValue + 1).fill(0);\n let totalCost = 0;\n\n for (let i = 0; i < instructions.length; i++) {\n const value = instructions[i];\n const lessCount = query(value - 1);\n const greaterCount = i - query(value);\n totalCost = (totalCost + Math.min(lessCount, greaterCount)) % MOD;\n update(value);\n }\n\n return totalCost;\n\n function update(index) {\n for (let i = index; i <= maxValue; i += i & -i) {\n fenwickTree[i]++;\n }\n }\n\n function query(index) {\n let sum = 0;\n for (let i = index; i > 0; i -= i & -i) {\n sum += fenwickTree[i];\n }\n return sum;\n }\n};","keywords":"LeetCode 1649, #1649 - Create Sorted Array through Instructions, 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/create-sorted-array-through-instructions |
| 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/create-sorted-array-through-instructions/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1649-create-sorted-array-through-instructions.js |
| Array | https://leetcodejavascript.com/tags/array |
| Binary Search | https://leetcodejavascript.com/tags/binary-search |
| Ordered Set | https://leetcodejavascript.com/tags/ordered-set |
| Segment Tree | https://leetcodejavascript.com/tags/segment-tree |
| Divide and Conquer | https://leetcodejavascript.com/tags/divide-and-conquer |
| Binary Indexed Tree | https://leetcodejavascript.com/tags/binary-indexed-tree |
| Merge Sort | https://leetcodejavascript.com/tags/merge-sort |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow