-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path360_Sort Transformed Array.java
More file actions
34 lines (32 loc) · 971 Bytes
/
360_Sort Transformed Array.java
File metadata and controls
34 lines (32 loc) · 971 Bytes
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
class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int[] res = new int[nums.length];
int left = 0, right = nums.length - 1;
int i = a >= 0? nums.length - 1: 0;
while (left <= right) {
int y1 = a * nums[left] * nums[left] + b * nums[left] + c;
int y2 = a * nums[right] * nums[right] + b * nums[right] + c;
if (a >= 0) {
if (y1 < y2) {
res[i--] = y2;
right--;
}
else {
res[i--] = y1;
left++;
}
}
else {
if (y1 > y2) {
res[i++] = y2;
right--;
}
else {
res[i++] = y1;
left++;
}
}
}
return res;
}
}