977. Squares of a Sorted Array
Problem Description
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Example 1:
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
Example 2:
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Constraints:
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4nums is sorted in non-decreasing order.
Solution
Python Solution
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
n = len(nums)
result = [0] * n
left, right = 0, n - 1
for i in range(n - 1, -1, -1):
if abs(nums[left]) > abs(nums[right]):
result[i] = nums[left] * nums[left]
left += 1
else:
result[i] = nums[right] * nums[right]
right -= 1
return result
Time Complexity: O(n)
We need to process each element exactly once.
Space Complexity: O(1)
We only use a constant amount of extra space.
Java Solution
class Solution {
public int[] sortedSquares(int[] nums) {
int n = nums.length;
int[] result = new int[n];
int left = 0, right = n - 1;
for (int i = n - 1; i >= 0; i--) {
if (Math.abs(nums[left]) > Math.abs(nums[right])) {
result[i] = nums[left] * nums[left];
left++;
} else {
result[i] = nums[right] * nums[right];
right--;
}
}
return result;
}
}
Time Complexity: O(n)
We need to process each element exactly once.
Space Complexity: O(1)
We only use a constant amount of extra space.
C++ Solution
class Solution {
public:
vector sortedSquares(vector& nums) {
int n = nums.size();
vector result(n);
int left = 0, right = n - 1;
for (int i = n - 1; i >= 0; i--) {
if (abs(nums[left]) > abs(nums[right])) {
result[i] = nums[left] * nums[left];
left++;
} else {
result[i] = nums[right] * nums[right];
right--;
}
}
return result;
}
};
Time Complexity: O(n)
We need to process each element exactly once.
Space Complexity: O(1)
We only use a constant amount of extra space.
JavaScript Solution
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortedSquares = function(nums) {
const n = nums.length;
const result = new Array(n);
let left = 0, right = n - 1;
for (let i = n - 1; i >= 0; i--) {
if (Math.abs(nums[left]) > Math.abs(nums[right])) {
result[i] = nums[left] * nums[left];
left++;
} else {
result[i] = nums[right] * nums[right];
right--;
}
}
return result;
};
Time Complexity: O(n)
We need to process each element exactly once.
Space Complexity: O(1)
We only use a constant amount of extra space.
C# Solution
public class Solution {
public int[] SortedSquares(int[] nums) {
int n = nums.Length;
int[] result = new int[n];
int left = 0, right = n - 1;
for (int i = n - 1; i >= 0; i--) {
if (Math.Abs(nums[left]) > Math.Abs(nums[right])) {
result[i] = nums[left] * nums[left];
left++;
} else {
result[i] = nums[right] * nums[right];
right--;
}
}
return result;
}
}
Time Complexity: O(n)
We need to process each element exactly once.
Space Complexity: O(1)
We only use a constant amount of extra space.