Sort Transformed Array
Description
null
Solution(javascript)
/**
* @param {number[]} nums
* @param {number} a
* @param {number} b
* @param {number} c
* @return {number[]}
*/
var sortTransformedArray = function(nums, a, b, c) {
let left = 0
let right = nums.length - 1
const result = []
const getValue = (x) => a * x * x + b * x + c
while(left <= right) {
const leftValue = getValue(nums[left])
const rightValue = getValue(nums[right])
if(a > 0) {
if(leftValue > rightValue) {
result.push(leftValue)
left++
} else {
result.push(rightValue)
right--
}
} else {
if(leftValue < rightValue) {
result.push(leftValue)
left++
} else {
result.push(rightValue)
right--
}
}
}
return a > 0 ? result.reverse() : result
};