Skip to content

Instantly share code, notes, and snippets.

@MohammedALREAI
Created April 17, 2022 08:22
Show Gist options
  • Save MohammedALREAI/00f696845230274b8d18613068faee11 to your computer and use it in GitHub Desktop.
Save MohammedALREAI/00f696845230274b8d18613068faee11 to your computer and use it in GitHub Desktop.
leetcode 977. Squares of a Sorted Array
function sortedSquares(nums: number[]): number[] {
let result = Array(nums.length).fill(0)
let p1 = 0
let p2 = nums.length - 1
let position = p2
while (p1 <= p2) {
let smaller=nums[p1]
let laege=nums[p2]
if( Math.abs(smaller)>Math.abs(laege)){
result[position] = smaller*smaller
position--;
p1++
} else {
result[position] = laege*laege
position--
p2--
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment