Skip to content

Commit 59db41f

Browse files
committed
feat: add squares of a sorted array solution
1 parent 2147683 commit 59db41f

File tree

4 files changed

+127
-7
lines changed

4 files changed

+127
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
## Easy
44

5-
| # | Problem | Solution | Time | Space | Tag |
6-
| --- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------------------- |
7-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/twoSum/README.md) | _O(n)_ | _O(n)_ | Array, Hash Table |
8-
| 2 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/moveZeroes/README.md) | _O(n)_ | _O(1)_ | Array, Two Pointers |
9-
| 3 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/validPalindrome/README.md) | _O(n)_ | _O(n)_ | String, Two Pointers |
10-
| 4 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/summaryRanges/README.md) | _O(n)_ | _O(n)_ | Array |
11-
| 5 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/mergeTwoSortedLists/README.md) | _O(n)_ | _O(n)_ | Linked List, Recursion |
5+
| # | Problem | Solution | Time | Space | Tag |
6+
| --- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------ | ------ | ---------------------------- |
7+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/twoSum/README.md) | _O(n)_ | _O(n)_ | Array, Hash Table |
8+
| 2 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/moveZeroes/README.md) | _O(n)_ | _O(1)_ | Array, Two Pointers |
9+
| 3 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/validPalindrome/README.md) | _O(n)_ | _O(n)_ | String, Two Pointers |
10+
| 4 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/summaryRanges/README.md) | _O(n)_ | _O(n)_ | Array |
11+
| 5 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/mergeTwoSortedLists/README.md) | _O(n)_ | _O(n)_ | Linked List, Recursion |
12+
| 6 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/squaresOfaSortedArray/README.md) | _O(n)_ | _O(1)_ | Array, Two Pointers, Sorting |
1213

1314
## Medium
1415

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/)
2+
3+
## Description
4+
5+
Given an integer array **nums** sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
6+
7+
**Example 1:**
8+
9+
```
10+
Input: nums = [-4,-1,0,3,10]
11+
Output: [0,1,9,16,100]
12+
Explanation: After squaring, the array becomes [16,1,0,9,100].
13+
After sorting, it becomes [0,1,9,16,100].
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: nums = [-7,-3,2,3,11]
20+
Output: [4,9,9,49,121]
21+
```
22+
23+
## Solution
24+
25+
```typescript
26+
function sortedSquares(nums: number[]): number[] {
27+
const result: number[] = [];
28+
29+
let left = 0;
30+
let right = nums.length - 1;
31+
32+
while (left <= right) {
33+
const leftSquare = nums[left] * nums[left];
34+
const rightSquare = nums[right] * nums[right];
35+
36+
if (leftSquare >= rightSquare) {
37+
result.unshift(leftSquare);
38+
left++;
39+
} else {
40+
result.unshift(rightSquare);
41+
right--;
42+
}
43+
}
44+
45+
return result;
46+
}
47+
```
48+
49+
## Complexity Analysis
50+
51+
- Time complexity: _O(n)_
52+
53+
- Space complexity: _O(1)_
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { sortedSquares } from "./sortedSquares";
2+
3+
describe("Squares of a Sorted Array", () => {
4+
it("should return squares of sorted array when input contains positive and negative integers", () => {
5+
const nums = [-4, -1, 0, 3, 10];
6+
const expected = [0, 1, 9, 16, 100];
7+
const result = sortedSquares(nums);
8+
expect(result).toEqual(expected);
9+
});
10+
11+
it("should return squares of sorted array when input contains all positive integers", () => {
12+
const nums = [1, 2, 3, 4, 5];
13+
const expected = [1, 4, 9, 16, 25];
14+
const result = sortedSquares(nums);
15+
expect(result).toEqual(expected);
16+
});
17+
18+
it("should return squares of sorted array when input contains all negative integers", () => {
19+
const nums = [-5, -4, -3, -2, -1];
20+
const expected = [1, 4, 9, 16, 25];
21+
const result = sortedSquares(nums);
22+
expect(result).toEqual(expected);
23+
});
24+
25+
it("should return squares of sorted array when input contains a mix of positive and negative integers", () => {
26+
const nums = [-7, -3, -1, 4, 5, 9];
27+
const expected = [1, 9, 16, 25, 49, 81];
28+
const result = sortedSquares(nums);
29+
expect(result).toEqual(expected);
30+
});
31+
32+
it("should return squares of sorted array when input contains a single element", () => {
33+
const nums = [0];
34+
const expected = [0];
35+
const result = sortedSquares(nums);
36+
expect(result).toEqual(expected);
37+
});
38+
39+
it("should return an empty array when input is an empty array", () => {
40+
const nums: number[] = [];
41+
const expected: number[] = [];
42+
const result = sortedSquares(nums);
43+
expect(result).toEqual(expected);
44+
});
45+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function sortedSquares(nums: number[]): number[] {
2+
const result: number[] = [];
3+
4+
let left = 0;
5+
let right = nums.length - 1;
6+
7+
while (left <= right) {
8+
const leftSquare = nums[left] * nums[left];
9+
const rightSquare = nums[right] * nums[right];
10+
11+
if (leftSquare >= rightSquare) {
12+
result.unshift(leftSquare);
13+
left++;
14+
} else {
15+
result.unshift(rightSquare);
16+
right--;
17+
}
18+
}
19+
20+
return result;
21+
}

0 commit comments

Comments
 (0)