Squares of a Sorted Array
EasyPython 3Two pointersArraysSorting
You are given a list of integers nums sorted in non-decreasing order. Return a list of the squares of those integers, also sorted in non-decreasing order.
Sorting the squares is the obvious answer at O(n log n); the interesting solution runs in O(n) by exploiting the fact that the input is already sorted.
Constraints: - 0 <= len(nums) <= 10^4 - -10^4 <= nums[i] <= 10^4 - nums is sorted in non-decreasing order
Examples: [-4, -1, 0, 3, 10] -> [0, 1, 9, 16, 100] [-7, -3, 2, 3, 11] -> [4, 9, 9, 49, 121] [1, 2, 3] -> [1, 4, 9] [] -> []
Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…