-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem69.js
65 lines (57 loc) · 1.84 KB
/
Problem69.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Problem 69
//
// This problem was asked by Facebook.
//
// Given a list of integers, return the largest product that can be made by multiplying any three integers.
//
// For example, if the list is [-10, -10, 5, 2], we should return 500, since that's -10 * -10 * 5.
//
// You can assume the list has at least three integers.
//
// https://leetcode.com/problems/maximum-product-of-three-numbers/
//
// O(N) Time complexity
// O(1) Space complexity
// N is the number of elements in the array
/**
* Returns the largest product that can be made by multiplying any three integers
* @param {number[]} nums
* @return {number}
*/
function maximumProduct(nums) {
// Two ways to get the maximum product:
// 1. Multiply the three largest numbers
// 2. The two smallest numbers multiplied with the largest number. (Assuming the two smallest are negative numbers)
let largest = Number.MIN_SAFE_INTEGER;
let secondLargest = Number.MIN_SAFE_INTEGER;
let thirdLargest = Number.MIN_SAFE_INTEGER;
let smallest = Number.MAX_SAFE_INTEGER;
let secondSmallest = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
// Setting the three largest numbers
// Moving the numbers, so they do not get replaced
if (num > largest) {
thirdLargest = secondLargest;
secondLargest = largest;
largest = num;
} else if (num > secondLargest) {
thirdLargest = secondLargest;
secondLargest = num;
} else if (num > thirdLargest) {
thirdLargest = num;
}
// Setting the two smallest numbers
if (num < smallest) {
secondSmallest = smallest;
smallest = num;
} else if (num < secondSmallest) {
secondSmallest = num;
}
}
return Math.max(
largest * secondLargest * thirdLargest,
smallest * secondSmallest * largest
);
}
export default maximumProduct;