Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created June 30, 2023 22:13
Show Gist options
  • Save Ifihan/523a0774015f83ff870aa13f41dbb757 to your computer and use it in GitHub Desktop.
Save Ifihan/523a0774015f83ff870aa13f41dbb757 to your computer and use it in GitHub Desktop.
Squares of a Sorted Array

Squares of a Sorted Array

Question on Leetcode - Easy

Approach

The approach to this question looks straighforward and simple at first. I'm returning the list pf squared arrays using sorted(). Should solve better tomorrow.

Code

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        return sorted([x ** 2 for x in nums])

Complexities

  • Time complexity: O(n)
  • Space complexity: O(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment