Squares of a Sorted Array
Easy
LC #977
sortingopposite endsNot attempted yet
nums is sorted in non-decreasing order and may contain
negative numbers. Return a new list with the square of each
number, also sorted in non-decreasing order.
Example 1
Input: nums = [-4, -1, 0, 3, 10]
Output: [0, 1, 9, 16, 100]
Example 2
Input: nums = [-7, -3, 2, 3, 11]
Output: [4, 9, 9, 49, 121]
Example 3
Input: nums = [-5, -3, -1]
Output: [1, 9, 25]
Constraints
- 1 ≤ len(nums) ≤ 2 · 10⁵
- -10⁵ ≤ nums[i] ≤ 10⁵
- Follow-up: O(n) without sorting the squares