-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1201.py
More file actions
43 lines (39 loc) · 996 Bytes
/
problem1201.py
File metadata and controls
43 lines (39 loc) · 996 Bytes
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
class Solution(object):
def nthUglyNumber(self, n, a, b, c):
"""
:type n: int
:type a: int
:type b: int
:type c: int
:rtype: int
"""
cnt = {a:1,b:1,c:1}
rst = set([])
res = 0
while n > 0:
currentA = a * cnt[a]
currentB = b * cnt[b]
currentC = c * cnt[c]
tmp = 0
if currentA <= currentB and currentA <= currentC:
tmp = currentA
cnt[a] += 1
elif currentB < currentA and currentB <= currentC:
tmp = currentB
cnt[b] += 1
elif currentC < currentA and currentC < currentB:
tmp = currentC
cnt[c] += 1
if tmp in rst:
n += 1
else:
res = tmp
rst.add(tmp)
n -= 1
return res
s = Solution()
n = 3
a = 2
b = 3
c = 5
print s.nthUglyNumber(n,a,b,c)