Skip to content

Commit 75b3b36

Browse files
committed
401. Binary Watch
1 parent 567984a commit 75b3b36

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [401. Binary Watch](https://leetcode.com/problems/binary-watch/description/) [🟢](## "Easy")
2+
3+
## Description:
4+
5+
A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
6+
7+
For example, the below binary watch reads `4:51`.
8+
9+
0 1 0 0
10+
1 1 0 0 1 1
11+
12+
Given an integer `turnedOn` which represents the number of LEDs that are currently on, return all possible times the watch could represent. You may return the answer in any order.
13+
14+
The hour must not contain a leading zero.
15+
16+
* For example, `"01:00"` is not valid. It should be `"1:00"`.
17+
18+
The minute must consist of two digits and may contain a leading zero.
19+
20+
* For example, `"10:2"` is not valid. It should be `"10:02"`.
21+
22+
23+
Example 1:
24+
25+
Input: turnedOn = 1
26+
Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
27+
28+
Example 2:
29+
30+
Input: turnedOn = 9
31+
Output: []
32+
33+
34+
Constraints:
35+
36+
0 <= turnedOn <= 10
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number} turnedOn
3+
* @return {string[]}
4+
*/
5+
export default function readBinaryWatch(turnedOn) {
6+
// if (turnedOn === 0) return ['0:00'];
7+
// if (turnedOn > 8) return [];
8+
const countBits = (dec) => dec.toString(2).split('').reduce((a, b) => a + parseInt(b), 0);
9+
const results = [];
10+
11+
for (let h = 0; h < 12; h++) {
12+
13+
const hbits = countBits(h);
14+
for (let m = 0; m < 60; m++) {
15+
const mbits = countBits(m);
16+
if (hbits + mbits === turnedOn) {
17+
results.push(`${h}:${m.toString().padStart(2, '0')}`);
18+
}
19+
}
20+
}
21+
22+
return results;
23+
};
24+
25+
// TODO: Why is this a better solution?
26+
// /**
27+
// * @param {number} turnedOn
28+
// * @return {string[]}
29+
// */
30+
// export default function readBinaryWatch(turnedOn) {
31+
// let res = [];
32+
// let count = (n) => n ? (n % 2) + count(n >> 1) : 0;
33+
// for (let h = 0; h < 12; h++) {
34+
// for (let m = 0; m < 60; m++) {
35+
// if (count(h) + count(m) == turnedOn) {
36+
// res.push(`${h}:${m < 10 ? '0' + m : m}`);
37+
// }
38+
// }
39+
// }
40+
// return res;
41+
// };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import solution from './solution';
2+
3+
describe('401. Binary Watch', () => {
4+
Object.entries({
5+
0: ['0:00'],
6+
1: ['0:01','0:02','0:04','0:08','0:16','0:32','1:00','2:00','4:00','8:00'],
7+
8: ['7:31','7:47','7:55','7:59','11:31','11:47','11:55','11:59'],
8+
// 9: [],
9+
}).forEach(([input, expected], i) => {
10+
it(`Case ${i} (input = ${input})`, () => {
11+
expect(solution(parseInt(input))).toStrictEqual(expected);
12+
});
13+
})
14+
});

0 commit comments

Comments
 (0)