-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQuadraticApplication.java
54 lines (45 loc) · 1.23 KB
/
QuadraticApplication.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
/*https://binarysearch.com/problems/Quadratic-Application*/
import java.util.*;
class Solution {
public int[] solve(int[] nums, int a, int b, int c) {
int n = nums.length, i, breakP = 0;
int[] sq = new int[n];
for (i = 0; i < n; ++i)
{
sq[i] = nums[i]*nums[i];
if (nums[i] <= 0)
breakP = i;
}
int[] result = new int[n];
int start = 0, end = n, startVal = 0, endVal = 0;
i = 0;
while (end > start)
{
startVal = a*sq[start]+b*nums[start]+c;
endVal = a*sq[end-1]+b*nums[end-1]+c;
if ((a >= 0 && startVal >= endVal) || (a < 0 && startVal <= endVal))
{
result[i++] = startVal;
++start;
}
else
{
result[i++] = endVal;
--end;
}
}
if (a >= 0)
{
start = 0;
end = n-1;
while (start < end)
{
startVal = result[start];
result[start] = result[end];
result[end] = startVal;
++start; --end;
}
}
return result;
}
}