Created
January 19, 2021 08:29
-
-
Save charlieInDen/c7012288f2f1d8fcfa82ff2a62b12a6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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