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:

  1. Input: [-4,-1,0,3,10]
  2. Output: [0,1,9,16,100]
  1. Input: [-7,-3,2,3,11]
  2. Output: [4,9,9,49,121]

Solution:

  1. /**
  2. * @param {number[]} A
  3. * @return {number[]}
  4. */
  5. var sortedSquares = function(A) {
  6. return A.map(i=>i*i).sort((a,b)=>a-b);
  7. };

Runtime: 172 ms, faster than 100.00% of JavaScript online submissions for Squares of a Sorted Array.