-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2094-finding-3-digit-even-numbers.js
More file actions
51 lines (43 loc) · 1.39 KB
/
2094-finding-3-digit-even-numbers.js
File metadata and controls
51 lines (43 loc) · 1.39 KB
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
/**
* 2094. Finding 3-Digit Even Numbers
* https://leetcode.com/problems/finding-3-digit-even-numbers/
* Difficulty: Easy
*
* You are given an integer array digits, where each element is a digit. The array may contain
* duplicates.
*
* You need to find all the unique integers that follow the given requirements:
* - The integer consists of the concatenation of three elements from digits in any arbitrary order.
* - The integer does not have leading zeros.
* - The integer is even.
*
* For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.
*
* Return a sorted array of the unique integers.
*/
/**
* @param {number[]} digits
* @return {number[]}
*/
var findEvenNumbers = function(digits) {
const frequency = new Array(10).fill(0);
const uniqueNumbers = new Set();
for (const digit of digits) {
frequency[digit]++;
}
for (let hundreds = 1; hundreds <= 9; hundreds++) {
if (frequency[hundreds] === 0) continue;
frequency[hundreds]--;
for (let tens = 0; tens <= 9; tens++) {
if (frequency[tens] === 0) continue;
frequency[tens]--;
for (let ones = 0; ones <= 8; ones += 2) {
if (frequency[ones] === 0) continue;
uniqueNumbers.add(hundreds * 100 + tens * 10 + ones);
}
frequency[tens]++;
}
frequency[hundreds]++;
}
return Array.from(uniqueNumbers).sort((a, b) => a - b);
};