Created
October 6, 2024 17:32
-
-
Save mohneesh7/58c73775bf2c320da1e3e61222d57b2d to your computer and use it in GitHub Desktop.
Solution for Squares of a Sorted Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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