-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0401-binary-watch.js
More file actions
28 lines (24 loc) · 943 Bytes
/
0401-binary-watch.js
File metadata and controls
28 lines (24 loc) · 943 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
/**
* Binary Watch
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
var readBinaryWatch = function (turnedOn) {
const allPossibleTimes = [];
for (let hourValue = 0; hourValue < 12; hourValue++) {
for (let minuteValue = 0; minuteValue < 60; minuteValue++) {
const hourSetBits = hourValue.toString(2).split('1').length - 1;
const minuteSetBits = minuteValue.toString(2).split('1').length - 1;
const totalOnLeds = hourSetBits + minuteSetBits;
if (totalOnLeds === turnedOn) {
let minuteDisplayString = minuteValue.toString();
if (minuteValue < 10) {
minuteDisplayString = '0' + minuteDisplayString;
}
const formattedTimeString = `${hourValue}:${minuteDisplayString}`;
allPossibleTimes.push(formattedTimeString);
}
}
}
return allPossibleTimes;
};