-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path401. Binary Watch.py
54 lines (42 loc) · 1.88 KB
/
401. Binary Watch.py
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
47
48
49
50
51
52
53
54
class Solution:
def readBinaryWatch(self, turnedOn: int) -> List[str]:
'''
#bit-Manipulation Iteratively
def countBits(num):
count = 0
while num>0:
lastBit = num & 1
if lastBit: count+=1
num = num >> 1
return count
ans = []
for hr in range(0,12):
for mnt in range(0,60):
if (countBits(hr) + countBits(mnt))==turnedOn:
hour,minute = str(hr), "0" + str(mnt)
res = hour + ":" + minute[-2:]
ans.append(res)
return ans
'''
#BackTracking
time_representation = "0000000000"
def solver(turnedOn, time_representation, startAt):
if turnedOn==0:
hours = int(time_representation[:4],2)
minutes = int(time_representation[4:],2)
if hours < 12 and minutes < 60: #save the valid hour and minute combinations only!
minutes = "0" + str(minutes)
res = str(hours) + ":" + minutes[-2:]
answer.append(res)
return answer
else:
for i in range(startAt, 10):
if time_representation[i]=="0":
#change one bit and explore --- then backtrack
time_representation = time_representation[:i] + "1" + time_representation[i+1:]
solver(turnedOn-1, time_representation,i)
time_representation = time_representation[:i] + "0" + time_representation[i+1:]
return answer
answer = []
if turnedOn > 8: return answer
return solver(turnedOn, time_representation, 0)