Clean python solution with two pointers and merge


  • 1
    S
    class Solution(object):
        def sortTransformedArray(self, nums, a, b, c):
    
            def f(x): return a*x*x + b*x + c
                
            res = []
            low, high = 0, len(nums)-1
            
            while low <= high:
                f_low, f_high = f(nums[low]), f(nums[high])
                if (a > 0) ^ (f_low > f_high):
                    res.append(f_high)
                    high -= 1
                else:
                    res.append(f_low)
                    low += 1
            return res[::-1] if a > 0 else res
                    
    

    The solution combine the case where a==0 or b==0 into the else condition.


  • 0
    D
    This post is deleted!

Log in to reply
 

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