Skip to content

Commit

Permalink
0977-squares-of-a-sorted-array.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
andynullwong committed Jan 2, 2023
1 parent b393feb commit eda7d6f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions typescript/0977-squares-of-a-sorted-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function sortedSquares(nums: number[]): number[] {
let left: number = 0;
let right: number = nums.length - 1;

const answer: number[] = [];

while (left <= right) {
const leftSqr = nums[left] * nums[left];
const rightSqr = nums[right] * nums[right];

if (leftSqr > rightSqr) {
answer.push(leftSqr);
left++;
} else {
answer.push(rightSqr);
right--;
}
}
return answer.reverse();
}

0 comments on commit eda7d6f

Please sign in to comment.