Leetcode Problem 360. Sort Transformed Array

360. Sort Transformed Array

Leetcode Solutions

Two Pointers Approach

  1. Define a function transform that applies the quadratic function to a given x.
  2. Initialize an empty array answer to store the transformed elements in sorted order.
  3. Initialize two pointers left = 0 and right = nums.length - 1.
  4. If a is less than 0, iterate while left <= right: a. Apply transform to nums[left] and nums[right]. b. If the transformed value at left is less, append it to answer and increment left. c. Otherwise, append the transformed value at right to answer and decrement right.
  5. If a is greater than or equal to 0, iterate while left <= right: a. Apply transform to nums[left] and nums[right]. b. If the transformed value at left is greater, append it to the start of answer and increment left. c. Otherwise, append the transformed value at right to the start of answer and decrement right.
  6. Return the answer array.
UML Thumbnail

Naive Sorting Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...