LeetCode977 Squares of a Sorted Array
Author: Stefan Su
Create time: 2022-10-26 21:52:25
Location: New York City, NY, USA
Description Easy
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]
Constrains
1 <= nums.length <= 10^4
-10^4 <= nums[i] <= 10^4
nums
is sorted in non-decreasing order.
Analysis
- Brute force
- We can square each element in the original array and then sort this array using python built-in function
sorted()
.
- We can square each element in the original array and then sort this array using python built-in function
- Two indices, without definition of the length of
new_nums
list- There is total two indices, one from the front and another from the last. Compare the value of square in each index position. The larger one will be insert into the beginning of
new_nums
list. If size of thisnew_nums
list is not defined, then the operationnew_nums = [nums[j] ** 2] + new_nums
costs lots of computation time. But still, acceptable.
- There is total two indices, one from the front and another from the last. Compare the value of square in each index position. The larger one will be insert into the beginning of
- Two indices, with definition of the length of
new_nums
list- Almost same as last solution but define the size of
new_nums
list and initialize it at the very beginning. The advantage of this operation is that we can reduce running time largely just by replacing the corresponding number at wanted index.
- Almost same as last solution but define the size of
Solution
- Version 1: Brute force
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
# brute force
for i in range(len(nums)):
nums[i] = nums[i] ** 2
return sorted(nums)
- Version 2: Two indices, without definition of the length of
new_nums
list
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
# two index, without definition of the length of new_nums
new_nums = []
i = 0
j = len(nums) - 1
while i <= j:
if nums[i] ** 2 < nums[j] ** 2:
new_nums = [nums[j] ** 2] + new_nums
j -= 1
if nums[i] ** 2 >= nums[j] ** 2:
new_nums = [nums[i] ** 2] + new_nums
i += 1
return new_nums
- Version 3: Two index, with definition of the length of
new_nums
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
# two index, with definition of the length of new_nums
n = len(nums)
i = 0
j = n - 1
k = n - 1
new_nums = [-1] * n
while i <= j:
left_square = nums[i] ** 2
right_square = nums[j] ** 2
if left_square < right_square:
new_nums[k] = right_square
j -= 1
if left_square >= right_square:
new_nums[k] = left_square
i += 1
k -= 1
return new_nums
Hopefully, this blog can inspire you when solving LeetCode977. For any questions, please comment below.