Skip to content

Commit

Permalink
feat: add maximum product three numbers solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrig committed Jul 28, 2023
1 parent 39aea3d commit c0069cc
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

## 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 + m)_ | _O(1)_ | 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(n)_ | Array, Two Pointers, Sorting |
| 7 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/reverseLinkedList/README.md) | _O(n)_ | _O(1)_ | Linked List, Recursion |
| 8 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/validParentheses/README.md) | _O(n)_ | _O(n)_ | String, Stack |
| # | 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 + m)_ | _O(1)_ | 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(n)_ | Array, Two Pointers, Sorting |
| 7 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/reverseLinkedList/README.md) | _O(n)_ | _O(1)_ | Linked List, Recursion |
| 8 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/validParentheses/README.md) | _O(n)_ | _O(n)_ | String, Stack |
| 9 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/maximumProductofThreeNumbers/README.md) | _O(n)_ | _O(1)_ | Array, Math, Sorting |

## Medium

Expand Down
51 changes: 51 additions & 0 deletions typescript/src/maximumProductofThreeNumbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/)

## Description

Given an integer array **nums**, find three numbers whose product is maximum and return the maximum product.

**Example 1:**

```
Input: nums = [1,2,3]
Output: 6
```

**Example 2:**

```
Input: nums = [1,2,3,4]
Output: 24
```

**Example 3:**

```
Input: nums = [-1,-2,-3]
Output: -6
```

## Solution

```typescript
function maximumProduct(nums: number[]): number {
// First sort the array in ascending order
nums.sort((a, b) => a - b);

const n = nums.length;

// Product of three maximum elements
const product1 = nums[n - 1] * nums[n - 2] * nums[n - 3];

// The product of two minimum elements and a maximum element
const product2 = nums[0] * nums[1] * nums[n - 1];

// Return the maximum of the two products found
return Math.max(product1, product2);
}
```

## Complexity Analysis

- Time complexity: _O(n)_
- Space complexity: _O(1)_
23 changes: 23 additions & 0 deletions typescript/src/maximumProductofThreeNumbers/maximumProduct.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { maximumProduct } from "./maximumProduct";

describe("Maximum Product of Three Numbers", () => {
it("should return the maximum product of three numbers in the array", () => {
const testCases = [
{ input: [1, 2, 3], output: 6 },
{ input: [1, 2, 3, 4], output: 24 },
{ input: [-1, -2, -3], output: -6 },
{ input: [-1, -2, -3, 0], output: 0 },
{ input: [10, -10, 1, 2, 3], output: 300 },
];

for (const testCase of testCases) {
const result = maximumProduct(testCase.input);
expect(result).toBe(testCase.output);
}
});

it("should return 0 if the array contains less than 3 elements", () => {
const result = maximumProduct([1, 2]);
expect(result).toBe(0);
});
});
15 changes: 15 additions & 0 deletions typescript/src/maximumProductofThreeNumbers/maximumProduct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function maximumProduct(nums: number[]): number {
// First sort the array in ascending order
nums.sort((a, b) => a - b);

const n = nums.length;

// Product of three maximum elements
const product1 = nums[n - 1] * nums[n - 2] * nums[n - 3];

// The product of two minimum elements and a maximum element
const product2 = nums[0] * nums[1] * nums[n - 1];

// Return the maximum of the two products found
return Math.max(product1, product2);
}

0 comments on commit c0069cc

Please sign in to comment.