8000 Merge pull request #786 from flick-23/master · vJechsmayr/PythonAlgorithms@4599ac0 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.

Commit 4599ac0

Browse files
authored
Merge pull request #786 from flick-23/master
Added solution for Problem #401 Binary Watch
2 parents 6a8c5cd + f74d562 commit 4599ac0

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

LeetCode/0401_Binary_Watch_.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Solution(object):
2+
def combination(self, n, k, inp, res):
3+
# inp is expected to be a list of size n
4+
#print n,k,inp,res
5+
if k == 0:
6+
res.append([i for i in inp])
7+
return
8+
if k > n:
9+
return
10+
#for i in range(1,n+1):
11+
inp[n-1] = 1
12+
self.combination(n-1,k-1, inp, res)
13+
inp[n-1] = 0
14+
self.combination(n-1, k, inp, res)
15+
16+
17+
def getHours(self, h):
18+
if h == 0:
19+
return ["0"]
20+
if h == 1:
21+
return ["1", "2", "4", "8"]
22+
if h == 2:
23+
return ["3", "5", "6", "9", "10"]
24+
if h == 3:
25+
return ["7", "11"]
26+
27+
def getMinutes(self, m):
28+
inp = [0,0,0,0,0,0]
29+
res = []
30+
self.combination(6, m, inp, res)
31+
mins = [1, 2, 4, 8, 16, 32]
32+
minutes = []
33+
for comb in res:
34+
i = 0
35+
mn = 0
36+
while i < 6:
37+
if comb[i] == 1:
38+
mn += mins[i]
39+
i += 1
40+
if mn > 59:
41+
continue
42+
if mn < 10:
43+
minutes.append("0" + str(mn))
44+
else:
45+
minutes.append(str(mn))
46+
return minutes
47+
48+
def readBinaryWatch(self, num):
49+
"""
50+
:type num: int
51+
:rtype: List[str]
52+
"""
53+
i = 0
54+
res = []
55+
56+
while i <= num:
57+
h = i
58+
m = num - i
59+
if h < 4 and m < 6:
60+
lh = self.getHours(h)
61+
#print lh
62+
lm = self.getMinutes(m)
63+
#print lm
64+
for hr in lh:
65+
for mn in lm:
66+
res.append(hr + ":" + mn)
67+
i += 1
68+
69+
return res

0 commit comments

Comments
 (0)
0