You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Solution 1: calculate the squared value of each element in the array, then sort the array. Time complexity is O(nlogn), space complexity is O(n)
array = [-4, 1, 10, 5, -2, 7, -3]
def sortedSquare1(values):
for i in range(len(values)):
values[i] = values[i] * values[i]
values.sort()
return values
# Solution 2: using two pointers, one for the left side of the array and one for the right side of the array. Time complexity is O(n), space complexity is O(n)