Skip to content

Commit

Permalink
feat: add squares of a sorted array solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrig committed Jul 23, 2023
1 parent 2147683 commit 59db41f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

## Easy

| # | Problem | Solution | Time | Space | Tag |
| --- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------------------- |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| # | Problem | Solution | Time | Space | Tag |
| --- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------ | ------ | ---------------------------- |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |

## Medium

Expand Down
53 changes: 53 additions & 0 deletions typescript/src/squaresOfaSortedArray/README.md
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 typescript/src/squaresOfaSortedArray/sortedSquares.test.ts
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);
});
});
21 changes: 21 additions & 0 deletions typescript/src/squaresOfaSortedArray/sortedSquares.ts
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;
}

0 comments on commit 59db41f

Please sign in to comment.