-
Notifications
You must be signed in to change notification settings - Fork 887
/
SortTransformedArray.swift
46 lines (41 loc) · 1.42 KB
/
SortTransformedArray.swift
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
/**
* Question Link: https://leetcode.com/problems/sort-transformed-array/
* Primary idea: Compare head and tail and put larger/smaller one to end/start of array,
* according to a is less than 0 or not
* Time Complexity: O(n), Space Complexity: O(1)
*/
class SortTransformedArray {
func sortTransformedArray(_ nums: [Int], _ a: Int, _ b: Int, _ c: Int) -> [Int] {
var res = [Int](repeating: 0, count: nums.count)
var left = 0
var right = res.count - 1
var index = a >= 0 ? nums.count - 1 : 0
while left <= right {
let leftVal = getRes(a, b, c, nums[left])
let rightVal = getRes(a, b, c, nums[right])
if a >= 0 {
if leftVal > rightVal {
res[index] = leftVal
left += 1
} else {
res[index] = rightVal
right -= 1
}
index -= 1
} else if a < 0 {
if leftVal < rightVal {
res[index] = leftVal
left += 1
} else {
res[index] = rightVal
right -= 1
}
index += 1
}
}
return res
}
private func getRes(_ a: Int, _ b: Int, _ c: Int, _ x: Int) -> Int {
return a * x * x + b * x + c
}
}