Two-Pointers Solution using Parabolic Pattern


  • 0
    U
    class Solution(object):
        def sortTransformedArray(self, nums, a, b, c):
            """
            :type nums: List[int]
            :type a: int
            :type b: int
            :type c: int
            :rtype: List[int]
            """
    
            nums = map(lambda x: a * x * x + b * x + c, nums)
            index = 0
            
            if a == 0:
                if b > 0:
                    return nums
                else:
                    return nums[::-1]
            else:
                low = 0
                high = len(nums) - 1
                ans = []
                while low <= high:
                    if (a > 0 and nums[low] >= nums[high]) or (a < 0 and nums[low] <= nums[high]):
                        ans.append(nums[low])
                        low += 1
                    else:
                        ans.append(nums[high])
                        high -= 1
                
                if a > 0:
                    return ans [::-1]
                else:
                    return ans
    

Log in to reply
 

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