Binary Watch LeetCode solution in Python
1# Runtime: 44 ms (Top 50.1%) | Memory: 16.36 MB (Top 42.0%)234class Solution:5def readBinaryWatch(self, turnedOn: int) -> List[str]:6output = []7# Loop through all possible combinations of hours and minutes and count the number of set bits8for h in range(12):9for m in range(60):10if (11bin(h).count("1") + bin(m).count("1") == turnedOn12): # Check if the number of set bits in hours and minutes equals the target number13output.append(14f"{h}:{m:02d}"15) # Add the valid combination of hours and minutes to the output list16return output