-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path360.cpp
36 lines (36 loc) · 1.33 KB
/
360.cpp
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
__________________________________________________________________________________________________
class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
vector<int> res;
priority_queue<int, vector<int>, greater<int>> q;
for (auto d : nums) {
q.push(a * d * d + b * d + c);
}
while (!q.empty()) {
res.push_back(q.top()); q.pop();
}
return res;
}
};
__________________________________________________________________________________________________
class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
int n = nums.size(), i = 0, j = n - 1;
vector<int> res(n);
int idx = a >= 0 ? n - 1 : 0;
while (i <= j) {
if (a >= 0) {
res[idx--] = cal(nums[i], a, b, c) >= cal(nums[j], a, b, c) ? cal(nums[i++], a, b, c) : cal(nums[j--], a, b, c);
} else {
res[idx++] = cal(nums[i], a, b, c) >= cal(nums[j], a, b, c) ? cal(nums[j--], a, b, c) : cal(nums[i++], a, b, c);
}
}
return res;
}
int cal(int x, int a, int b, int c) {
return a * x * x + b * x + c;
}
};
__________________________________________________________________________________________________