Squares of a Sorted Array
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number, also sorted in non-decreasing order.
Open official problem prompt ↗Produce the sorted list of squares of an already-sorted array in linear time.
Two lines of people ordered by height meeting at the door; you repeatedly admit whoever is taller of the two front-most, filling a hall from the back forward so the tallest stands at the far end.
- Input
- nums = [-4, -1, 0, 3, 10]
- Output
- [0, 1, 9, 16, 100]
- Why
- Squaring gives [16, 1, 0, 9, 100]; sorted non-decreasing that is [0, 1, 9, 16, 100].
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4nums is sorted in non-decreasing order