Skip to content

Instantly share code, notes, and snippets.

@charlieInDen
Created January 19, 2021 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlieInDen/c7012288f2f1d8fcfa82ff2a62b12a6c to your computer and use it in GitHub Desktop.
Save charlieInDen/c7012288f2f1d8fcfa82ff2a62b12a6c to your computer and use it in GitHub Desktop.
func sortedSquares(_ A: [Int]) -> [Int] {
var res = [Int](repeating: 0, count: A.count)
var index = A.count - 1
var left = 0
var right = A.count - 1
while left <= right {
var a = A[left] * A[left]
var b = A[right] * A[right]
if a > b {
res[index] = a
index -= 1
left += 1
} else {
res[index] = b
index -= 1
right -= 1
}
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment