-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path360-SortTransformedArray.go
More file actions
65 lines (57 loc) · 1.73 KB
/
360-SortTransformedArray.go
File metadata and controls
65 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
// 360. Sort Transformed Array
// 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 nums[i] in the array,
// and return the array in a sorted order.
// Example 1:
// Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
// Output: [3,9,15,33]
// Example 2:
// Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
// Output: [-23,-5,1,7]
// Constraints:
// 1 <= nums.length <= 200
// -100 <= nums[i], a, b, c <= 100
// nums is sorted in ascending order.
// Follow up: Could you solve it in O(n) time?
import "fmt"
func sortTransformedArray(nums []int, a int, b int, c int) []int {
res, cur := make([]int, len(nums)), 0
if a > 0 {
cur = len(res) - 1
}
for i, j := 0, len(nums) - 1; i <= j; {
r1 := a * nums[i] * nums[i] + b * nums[i] + c // f(x) = ax2 + bx + c
r2 := a * nums[j] * nums[j] + b * nums[j] + c
if a <= 0 {
if r1 < r2 {
res[cur] = r1
i++
} else {
res[cur] = r2
j--
}
cur++
} else {
if r1 > r2 {
res[cur] = r1
i++
} else {
res[cur] = r2
j--
}
cur--
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
// Output: [3,9,15,33]
fmt.Println(sortTransformedArray([]int{-4,-2,2,4}, 1, 3, 5)) // [3,9,15,33]
// Example 2:
// Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
// Output: [-23,-5,1,7]
fmt.Println(sortTransformedArray([]int{-4,-2,2,4}, -1, 3, 5)) // [-23,-5,1,7]
}