-
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 maximum product three numbers solution
- Loading branch information
Showing
4 changed files
with
100 additions
and
10 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,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
23
typescript/src/maximumProductofThreeNumbers/maximumProduct.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,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
15
typescript/src/maximumProductofThreeNumbers/maximumProduct.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,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); | ||
} |