One minute
Leetcode 360
The idea of this solution is to loop through nums
and do the quadratic equation f(x) = ax^2 + bx + c
where x = nums[i]
and then make nums[i]
the result of the ax^2 + bx + c
. After that we can sort and return nums
.
Solution Using Manual Sort
func sortTransformedArray(nums []int, a int, b int, c int) []int {
for i := 0; i < len(nums); i++ {
nums[i] = a*nums[i]*nums[i] + b*nums[i] + c
}
for i := 1; i < len(nums); i++ {
if i >= 1 && nums[i] < nums[i-1] {
nums[i], nums[i-1] = nums[i-1], nums[i]
i -= 2
}
}
return nums
}
The Same Solution But Using a Built In Sort:
func sortTransformedArray(nums []int, a int, b int, c int) []int {
for i := 0; i < len(nums); i++ {
nums[i] = a*nums[i]*nums[i] + b*nums[i] + c
}
sort.Ints(nums)
return nums
}
Read other posts