-
Notifications
You must be signed in to change notification settings - Fork 46
/
_360.java
47 lines (44 loc) · 1.32 KB
/
_360.java
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
/**
* LeetCode 360 - Sort Transformed Array
* <p>
* O(n)-time solution
*/
public class _360 {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int[] ans = new int[nums.length];
for (int i = 0, j = nums.length - 1, ptr = 0; i <= j; ) {
int f1 = f(nums[i], a, b, c);
int f2 = f(nums[j], a, b, c);
if (a < 0) {
// Two increasing sub-sequences
if (f1 < f2) {
ans[ptr++] = f1;
i++;
} else {
ans[ptr++] = f2;
j--;
}
} else {
// Two decreasing sub-sequences
if (f1 > f2) {
ans[ptr++] = f1;
i++;
} else {
ans[ptr++] = f2;
j--;
}
}
}
// Final check to see whether ans is accidently sorted in decreasing order.
if (ans[0] > ans[ans.length - 1])
for (int i = 0, j = ans.length - 1; i < j; i++, j--) {
int tmp = ans[i];
ans[i] = ans[j];
ans[j] = tmp;
}
return ans;
}
private int f(int x, int a, int b, int c) {
return a * x * x + b * x + c;
}
}