2025-09-22 19:06
Status: adult
Tags: leetcode leetcode-easy two-pointers google Leetcode
Squares of a Sorted Array
Code
import heapq
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = []
l, r = 0, len(nums) - 1
while l <= r:
if abs(nums[r]) > abs(nums[l]):
res.append(nums[r] ** 2)
r -= 1
else:
res.append(nums[l] ** 2)
l += 1
return res[::-1]
Explanation
- Since this is sorted in increasing order, we can use two pointers, one on the left and right.
- We check if the abs(nums[r]) > abs(nums[l]), and move the right pointer to the left and vice versa for the left.
- The result list will be in decreasing order so all we need to do is reverse it at the end