Back to course home
0% completed
Vote For New Content
Python Solution
Thai Minh
Sep 17, 2023
class Solution: def makeSquares(self, arr): n = len(arr) squares = [0 for x in range(n)] # TODO: Write your code here # front and back pointer, square both up and compare which one bigger will be appened left first and # increament/decreament appropriately if len(arr) < 2: return [arr[0]**2] f_ptr, b_ptr = 0, len(arr) - 1 curr_pos = len(squares) - 1 while f_ptr <= b_ptr: f_value = arr[f_ptr]**2 b_value = arr[b_ptr]**2 if f_value >= b_value: squares[curr_pos] = f_value f_ptr += 1 else: squares[curr_pos] = b_value b_ptr -= 1 curr_pos -= 1 return squares
0
0
Comments
Comments