Skip to content

Instantly share code, notes, and snippets.

@mohneesh7
Created October 6, 2024 17:32
Show Gist options
  • Save mohneesh7/58c73775bf2c320da1e3e61222d57b2d to your computer and use it in GitHub Desktop.
Save mohneesh7/58c73775bf2c320da1e3e61222d57b2d to your computer and use it in GitHub Desktop.
Solution for Squares of a Sorted Array
# Solution for Squares of a Sorted Array
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
left = 0
right = len(nums) - 1
result = []
while left <= right:
if (nums[left]**2) > (nums[right]**2):
result.append(nums[left]**2)
left += 1
else:
result.append(nums[right]**2)
right -= 1
result.reverse()
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment