class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
vector<int> res;
if (a == 0) {
for (int x: nums) res.push_back(b * x + c);
if (b < 0) reverse(res.begin(), res.end());
return res;
}
if (nums.empty()) return res;
double vert = double(b) / (2 * a);
int l = 0, r = nums.size()  1;
// merge from the extremes, the most far away from vert always first
while (l <= r) {
int x;
if (abs(nums[l]  vert) > abs(nums[r]  vert)) {
x = nums[l]; l++;
} else {
x = nums[r]; r;
}
res.push_back(a * x * x + b * x + c);
}
if (a > 0) reverse(res.begin(), res.end());
return res;
}
};
G
gastonbm
@gastonbm
6
Reputation
15
Posts
133
Profile views
0
Followers
0
Following
Posts made by gastonbm

Extremely simple solution using distance to parabola's vertex

RE: C++ solution 8ms
it's probably better to push_back your elements to res and then reverse res if a < 0 to avoid duplicating so much code.

RE: Simple C++ solution
I don't find it cool that you copy the vectors. If you're exposing an iterator interface for vector it is expected to work in O(1) space.. You could use a pointers to the vectors or iterators (you will need pointers to the end of each one also).

RE: C++ O(1)space with Explanations
p = (p + 1) % 2
is the same as
p = 1  p

RE: O(n) Time & O(1) Space Java Solution
In the case when length == 2, you can simply do
pos = 1  pos;

RE: O(n) Time & O(1) Space Java Solution
I think
pos = (pos == its.length  1) ? 0 : pos + 1;
can be stated more clearly as
pos = (pos + 1) % its.length;

RE: C++ solution simple and sweet
You can compress these two:
vector<int> res{l+1,r+1}; return res;
into:
return vector<int>{l+1, r+1};

RE: C++ 28ms simple solution with preserved vector
you never used the variable sum