classSolution { public: vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c){ int n = nums.size(), l = 0, r = n - 1, i = a < 0 ? 0 : n - 1; vector<int> res(n); auto f = [a, b, c](int x) { return (a * x + b) * x + c; }; while (l <= r) { int fl = f(nums[l]), fr = f(nums[r]); if (fl > fr) { if (a < 0) { res[i++] = fr; --r; } else { res[i--] = fl; ++l; } } else { if (a < 0) { res[i++] = fl; ++l; } else { res[i--] = fr; --r; } } } return res; } };
classSolution { public: vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c){ int t = -b; int n = nums.size(), l = 0, r = n - 1, i = a < 0 ? 0 : n - 1; vector<int> res(n); while (l <= r) { int s = a * (nums[l] + nums[r]); if (s < t) { if (a < 0) { res[i++] = nums[r--]; } else { res[i--] = nums[l++]; } } else { if (a < 0) { res[i++] = nums[l++]; } else { res[i--] = nums[r--]; } } } for (int &x : res) { x = (a * x + b) * x + c; } return res; } };
classSolution { public: vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c){ this->a = a; this->b = b; this->c = c; int t = -b; int n = nums.size(), l = 0, r = n - 1, i = a < 0 ? 0 : n - 1; vector<int> res(n); while (l <= r) { int s = a * (nums[l] + nums[r]); if (s < t) { if (a < 0) { res[i++] = f(nums[r--]); } else { res[i--] = f(nums[l++]); } } else { if (a < 0) { res[i++] = f(nums[l++]); } else { res[i--] = f(nums[r--]); } } } return res; }