Question:
Given an array of integers A
sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.
Example:
Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Solution:
/**
* @param {number[]} A
* @return {number[]}
*/
var sortedSquares = function(A) {
return A.map(i=>i*i).sort((a,b)=>a-b);
};
Runtime: 172 ms, faster than 100.00% of JavaScript online submissions for Squares of a Sorted Array.