class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
if len(nums) == 0:
return []
if len(nums) == 1:
return [nums[0] ** 2]
result = []
i = 0
square_less_zero = []
while i < len(nums) and nums[i] < 0:
square_less_zero.append(nums[i] ** 2)
i += 1
j = len(square_less_zero) - 1
while i < len(nums) and j >= 0:
if nums[i] ** 2 <= square_less_zero[j]:
result.append(nums[i] ** 2)
i += 1
else:
result.append(square_less_zero[j])
j -= 1
if i < len(nums):
while i < len(nums):
result.append(nums[i] ** 2)
i += 1
if j >= 0:
while j >= 0:
result.append(square_less_zero[j])
j -= 1
return result