Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
[Spoiler] My tidy solution

John O'Neill

Nov 26, 2023

I didn't want to recalculate the squares. :)

class Solution: def makeSquares(self, arr): n = len(arr) squares = [0] * n left, right, i = 0, n - 1, n - 1 sq_left = arr[left]**2 sq_right = arr[right]**2 while i >= 0: if sq_left > sq_right: squares[i] = sq_left left += 1 sq_left = arr[left]**2 else: squares[i] = sq_right right -= 1 sq_right = arr[right]**2 i -= 1 return squares

0

0

Comments
Comments
 Sri
Sri2 years ago

where are you saving the calculation of squares? I still see you are calculating squares..?

On this page

Problem Statement

Solution

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity