My simple c++ solution


  • 0
    K
    class Solution {
    private:
    int A, B, C;
     public:
    vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
        A=a;
        B=b;
        C=c;
        vector<int>res(nums.size(), 0);
        int start=0, end=nums.size()-1, k=nums.size()-1;
        if(a >= 0){
        while(start <= end){
            int a=fx(nums[start]), b=fx(nums[end]);
            if(a > b) {
                res[k--]=a;start++;
            }
            else {
                res[k--]=b;end--;
            }
          }
        }
        
        else{
            k=0;
            while(start <= end){
            int a=fx(nums[start]), b=fx(nums[end]);
            if(a < b) {
                res[k++]=a;start++;
            }
            else {
                res[k++]=b;end--;
            }
          }
        }
        
        return res;
    }
    int fx(int x){
        return A * x * x + B * x + C;
    }
    

    };


Log in to reply
 

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