-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
46 lines (41 loc) · 1.23 KB
/
solution.ts
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
function readBinaryWatch (turnedOn: number): string[] {
const HOURS = [8, 4, 2, 1, ];
const MINUTES = [32, 16, 8, 4, 2, 1, ];
const result:string[] = [];
for (let i = 0; i <= turnedOn; i++) {
const hours:number[] = [];
if (i === 0) {
hours.push(0);
} else {
backTracking(HOURS, 0, i, 0, 11, hours);
}
const minutes:number[] = [];
const minCount = turnedOn - i;
if (minCount === 0) {
minutes.push(0);
} else {
backTracking(MINUTES, 0, minCount, 0, 59, minutes);
}
for (const h of hours) {
for (const m of minutes) {
result.push(`${h}:${String(m).padStart(2, '0')}`);
}
}
}
return result;
}
function backTracking (candidates:number[], index:number, rest:number, sum:number, limit:number, result:number[]) {
if (rest === 0) {
result.push(sum);
return;
}
if (index === candidates.length) {
return;
}
backTracking(candidates, index + 1, rest, sum, limit, result);
sum += candidates[index];
if (sum > limit) {
return;
}
backTracking(candidates, index + 1, rest - 1, sum, limit, result);
}