forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_069.js
35 lines (30 loc) Β· 990 Bytes
/
problem_069.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// https://leetcode.com/problems/maximum-product-of-three-numbers/
// Algorithmic Paradigm: Math?
// O(N) Time complexity
// O(1) Space complexity
// N is the number of elements in the array
/**
* Largest product that can by made by multiplying 3 integers
* @param {number[]} numbers
* @return {number}
*/
function getLargestProduct(nums) {
// Sort array
nums.sort((a, b) => a - b);
let maxProd = 1;
let len = nums.length;
// Max Product can be found by:
// 1. Multiply the three largest numbers
// 2. 2. The two smallest numbers multiplied with the largest number.
// (Assuming the two smallest are negative numbers)
for (let i = 0; i < len; i++) {
maxProd = Math.max(
nums[0] * nums[1] * nums[len - 1],
nums[len - 3] * nums[len - 2] * nums[len - 1]
);
}
return maxProd;
}
console.log(getLargestProduct([-10, -10, 5, 2])); // 500
console.log(getLargestProduct([1, 1, 10])); // 10
console.log(getLargestProduct([-1, 0, 10, 100, 8])); // 8000