-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1913.两个数对之间的最大乘积差.js
More file actions
49 lines (48 loc) · 966 Bytes
/
1913.两个数对之间的最大乘积差.js
File metadata and controls
49 lines (48 loc) · 966 Bytes
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
/*
* @lc app=leetcode.cn id=1913 lang=javascript
*
* [1913] 两个数对之间的最大乘积差
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number}
*/
var maxProductDifference = function (nums) {
let w = 0, // 最大
x = 0, // 第二大
y = 10001, // 最小
z = 10001; // 第二小
for (let num of nums) {
// 大于最大值
if (num > w) {
if (w !== 0) {
// 更新第二大值
x = w;
}
w = num;
} else {
// 小于最大值,但是大于第二大值
if (num > x) {
x = num;
}
}
// 小于最小值
if (num < y) {
if (y !== 10001) {
// 更新第二小值
z = y;
}
y = num;
} else {
// 大于最大值,但是小于第二小值
if (num < z) {
z = num;
}
}
}
return w * x - y * z;
};
const nums = [5, 6, 2, 7, 4];
console.log(maxProductDifference(nums));
// @lc code=end