-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add squares of a sorted array solution
- Loading branch information
Showing
4 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | ||
|
||
## Description | ||
|
||
Given an integer array **nums** sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: nums = [-4,-1,0,3,10] | ||
Output: [0,1,9,16,100] | ||
Explanation: After squaring, the array becomes [16,1,0,9,100]. | ||
After sorting, it becomes [0,1,9,16,100]. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: nums = [-7,-3,2,3,11] | ||
Output: [4,9,9,49,121] | ||
``` | ||
|
||
## Solution | ||
|
||
```typescript | ||
function sortedSquares(nums: number[]): number[] { | ||
const result: number[] = []; | ||
|
||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
while (left <= right) { | ||
const leftSquare = nums[left] * nums[left]; | ||
const rightSquare = nums[right] * nums[right]; | ||
|
||
if (leftSquare >= rightSquare) { | ||
result.unshift(leftSquare); | ||
left++; | ||
} else { | ||
result.unshift(rightSquare); | ||
right--; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
``` | ||
|
||
## Complexity Analysis | ||
|
||
- Time complexity: _O(n)_ | ||
|
||
- Space complexity: _O(1)_ |
45 changes: 45 additions & 0 deletions
45
typescript/src/squaresOfaSortedArray/sortedSquares.test.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { sortedSquares } from "./sortedSquares"; | ||
|
||
describe("Squares of a Sorted Array", () => { | ||
it("should return squares of sorted array when input contains positive and negative integers", () => { | ||
const nums = [-4, -1, 0, 3, 10]; | ||
const expected = [0, 1, 9, 16, 100]; | ||
const result = sortedSquares(nums); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("should return squares of sorted array when input contains all positive integers", () => { | ||
const nums = [1, 2, 3, 4, 5]; | ||
const expected = [1, 4, 9, 16, 25]; | ||
const result = sortedSquares(nums); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("should return squares of sorted array when input contains all negative integers", () => { | ||
const nums = [-5, -4, -3, -2, -1]; | ||
const expected = [1, 4, 9, 16, 25]; | ||
const result = sortedSquares(nums); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("should return squares of sorted array when input contains a mix of positive and negative integers", () => { | ||
const nums = [-7, -3, -1, 4, 5, 9]; | ||
const expected = [1, 9, 16, 25, 49, 81]; | ||
const result = sortedSquares(nums); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("should return squares of sorted array when input contains a single element", () => { | ||
const nums = [0]; | ||
const expected = [0]; | ||
const result = sortedSquares(nums); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("should return an empty array when input is an empty array", () => { | ||
const nums: number[] = []; | ||
const expected: number[] = []; | ||
const result = sortedSquares(nums); | ||
expect(result).toEqual(expected); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export function sortedSquares(nums: number[]): number[] { | ||
const result: number[] = []; | ||
|
||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
while (left <= right) { | ||
const leftSquare = nums[left] * nums[left]; | ||
const rightSquare = nums[right] * nums[right]; | ||
|
||
if (leftSquare >= rightSquare) { | ||
result.unshift(leftSquare); | ||
left++; | ||
} else { | ||
result.unshift(rightSquare); | ||
right--; | ||
} | ||
} | ||
|
||
return result; | ||
} |