-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1363.形成三的最大倍数.js
More file actions
38 lines (35 loc) · 880 Bytes
/
Copy path1363.形成三的最大倍数.js
File metadata and controls
38 lines (35 loc) · 880 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
/**
* @param {number[]} digits
* @return {string}
*/
var largestMultipleOfThree = function(digits) {
digits.sort((a, b) => a - b);
const map = [[], [], []];
let sum = 0;
for (const n of digits) {
map[n % 3].push(n);
sum += n;
}
function formatResult() {
const arr = [...map[0], ...map[1], ...map[2]].sort((a, b) => b - a);
while (arr.length > 1 && arr[0] === 0) {
arr.shift();
}
return arr.join('');
}
if (sum % 3 === 0) {
return formatResult();
} else {
const a = sum % 3;
const b = a === 1 ? 2 : 1;
if (map[a].length >= 1) {
map[a].shift();
} else if (map[b].length >= 2) {
map[b].shift();
map[b].shift();
} else {
return '';
}
return formatResult();
}
};