forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_360.java
71 lines (57 loc) · 2.22 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.fishercoder.solutions;
import java.util.Arrays;
/**
* 360. Sort Transformed Array
*
* Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array.
The returned array must be in sorted order.
Expected time complexity: O(n)
Example:
nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5,
Result: [3, 9, 15, 33]
nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5
Result: [-23, -5, 1, 7]
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
*/
public class _360 {
public static class Solution1 {
//credit: https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution
//in sum, only two cases: when a >= 0 or when a < 0, this simplifies logic
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int n = nums.length;
int[] sorted = new int[n];
int i = 0;
int j = n - 1;
int index = a >= 0 ? n - 1 : 0;
while (i <= j) {
if (a >= 0) {
sorted[index--] =
function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(
nums[i++], a, b, c) : function(nums[j--], a, b, c);
} else {
sorted[index++] =
function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(
nums[j--], a, b, c) : function(nums[i++], a, b, c);
}
}
return sorted;
}
private int function(int num, int a, int b, int c) {
return a * (num * num) + b * num + c;
}
}
public static class Solution2 {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int[] result = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
result[i] = function(nums[i], a, b, c);
}
Arrays.sort(result);
return result;
}
private int function(int num, int a, int b, int c) {
return a * (num * num) + b * num + c;
}
}
}