Title: #360 - Sort Transformed Array - LeetCode JavaScript Solutions
Open Graph Title: #360 - Sort Transformed Array - LeetCode JavaScript Solutions
X Title: #360 - Sort Transformed Array - LeetCode JavaScript Solutions
Description: Given a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax2 + bx + c to each element ...
Open Graph Description: Given a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax2 + bx + c to each element ...
X Description: Given a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax2 + bx + c to each element ...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/sort-transformed-array
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/sort-transformed-array","name":"#360 - Sort Transformed Array - LeetCode JavaScript Solutions","description":"Given a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax2 + bx + c to each element ...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} nums\n * @param {number} a\n * @param {number} b\n * @param {number} c\n * @return {number[]}\n */\nvar sortTransformedArray = function(nums, a, b, c) {\n const result = new Array(nums.length);\n let left = 0;\n let right = nums.length - 1;\n let index = a >= 0 ? nums.length - 1 : 0;\n\n while (left <= right) {\n const leftVal = transform(nums[left]);\n const rightVal = transform(nums[right]);\n\n if (a >= 0) {\n if (leftVal > rightVal) {\n result[index--] = leftVal;\n left++;\n } else {\n result[index--] = rightVal;\n right--;\n }\n } else {\n if (leftVal < rightVal) {\n result[index++] = leftVal;\n left++;\n } else {\n result[index++] = rightVal;\n right--;\n }\n }\n }\n\n return result;\n\n function transform(x) {\n return a * x * x + b * x + c;\n }\n};","keywords":"LeetCode 360, #360 - Sort Transformed Array, Medium, 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/sort-transformed-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/sort-transformed-array/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0360-sort-transformed-array.js |
| Array | https://leetcodejavascript.com/tags/array |
| Math | https://leetcodejavascript.com/tags/math |
| Sorting | https://leetcodejavascript.com/tags/sorting |
| Two Pointers | https://leetcodejavascript.com/tags/two-pointers |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow