Concise 6ms C++ Solution


  • 0
    W
    class Solution {
    public:
        vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
            vector<int> res;
            if(a>0){
                vector<int> temp = sortTransformedArray(nums, -a, -b, -c);
                for(int i=0;i<nums.size();i++)
                    res.push_back(-temp[nums.size()-1-i]);
            }
            else{
                int i = 0;
                int j = nums.size()-1;
                while(i<=j){
                    int func1 = a*nums[i]*nums[i]+b*nums[i]+c;
                    int func2 = a*nums[j]*nums[j]+b*nums[j]+c;
                    
                    if(func1 < func2){
                        res.push_back(func1);
                        i++;
                    }
                    else{
                        res.push_back(func2);
                        j--;
                    }
                }
            }
            return res;
        }
    };
    

Log in to reply
 

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