-
Notifications
You must be signed in to change notification settings - Fork 2
/
2815.数组中的最大数对和.html
59 lines (49 loc) · 1.67 KB
/
2815.数组中的最大数对和.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>2815. 数组中的最大数对和</title>
</head>
<body>
<script>
// https://leetcode.cn/problems/max-pair-sum-in-an-array/description/
// 给你一个下标从 0 开始的整数数组 nums 。请你从 nums 中找出和 最大 的一对数,且这两个数数位上最大的数字相等。
// 返回最大和,如果不存在满足题意的数字对,返回 -1 。
//
// 提示:
// 2 <= nums.length <= 100
// 1 <= nums[i] <= 104
/**
* @param {number[]} nums
* @return {number}
*/
var maxSum = function (nums) {};
// --- answer-1 ---
var maxSum = function (nums) {
let ans = -1;
let maxVal = Array(10).fill(Number.MIN_SAFE_INTEGER);
for (const v of nums) {
let maxD = 0;
for (let x = v; x; x = Math.floor(x / 10)) maxD = Math.max(maxD, x % 10);
ans = Math.max(ans, v + maxVal[maxD]);
maxVal[maxD] = Math.max(maxVal[maxD], v);
}
return ans;
};
// --- answer-1 ---
// --- answer-2 ---
// --- answer-2 ---
// var nums = [51,71,17,24,42];
// var result = 88;
// 解释:
// var nums = [1,2,3,4];
// var result = -1;
// 解释:不存在数对满足数位上最大的数字相等。
console.log('nums = ', nums);
console.log('result = ', result);
console.log('maxSum = ', maxSum(nums));
</script>
</body>
</html>