A concise C++ solution


  • 0
    G
    class Solution {
    public:
        vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
            auto f = [=](int x) { return a * x * x + b * x + c; };
            int n = (int)nums.size(), l = 0, r = n - 1, i = 0, left = 0, right = 0;
            vector<int> result(n);
            while (l <= r) {
                left = f(nums[l]), right = f(nums[r]);
                result[a >= 0 ? n - ++i : i++] = a >= 0 ? max(left, right) : min(left, right);
                l += (a >= 0 && left > right) || (a < 0 && left < right);
                r -= (a >= 0 && left <= right) || (a < 0 && left >= right);
            }
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.