| title | Squares of a Sorted Array | |||
|---|---|---|---|---|
| difficulty | 🟢 Easy | |||
| tags |
|
|||
| url | https://leetcode.com/problems/squares-of-a-sorted-array/ |
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Example 1:
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
Example 2:
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4numsis sorted in non-decreasing order.
Follow up: Squaring each element and sorting the new array is very trivial, could you find an
The array contains negative numbers, and squaring them can produce large values. Key insight: the largest squares are at the extremes (far left for large negative numbers, far right for large positive numbers).
By using two pointers from both ends, we can build the result array from largest to smallest, then reverse it.
- Initialize
left = 0andright = len(nums) - 1 - Initialize empty
squareslist - While
left <= right:- Compare
abs(nums[left])withabs(nums[right]) - Append the larger square to
squares - Move the corresponding pointer inward
- Compare
- Reverse
squaresto get ascending order
-
Time Complexity:
$O(n)$ — Single pass with two pointers, plus$O(n)$ for reverse. -
Space Complexity:
$O(n)$ — For the result array.
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
squares = []
left = 0
right = len(nums) - 1
while left <= right:
if abs(nums[left]) >= abs(nums[right]):
squares.append(nums[left] ** 2)
left += 1
else:
squares.append(nums[right] ** 2)
right -= 1
squares.reverse()
return squares