Binary Watch LeetCode solution in Python

1
# Runtime: 44 ms (Top 50.1%) | Memory: 16.36 MB (Top 42.0%)
2

3

4
class Solution:
5
def readBinaryWatch(self, turnedOn: int) -> List[str]:
6
output = []
7
# Loop through all possible combinations of hours and minutes and count the number of set bits
8
for h in range(12):
9
for m in range(60):
10
if (
11
bin(h).count("1") + bin(m).count("1") == turnedOn
12
): # Check if the number of set bits in hours and minutes equals the target number
13
output.append(
14
f"{h}:{m:02d}"
15
) # Add the valid combination of hours and minutes to the output list
16
return output

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0