High efficence Python, find minimum of multiple of 2,3 and 5 as new element.


  • 0
    W
    class Solution(object):
        def nthUglyNumber(self, n):
            """
            :type n: int
            :rtype: int
            """
            ug,ct,i2,i3,i5=[0,1],1,1,1,1
            while ct<=n:
                while 2*ug[i2]<=ug[-1]:
                    i2+=1
                while 3*ug[i3]<=ug[-1]:
                    i3+=1
                while 5*ug[i5]<=ug[-1]:
                    i5+=1
                ug.append(min(2*ug[i2],3*ug[i3],5*ug[i5]))
                ct+=1
            return ug[n]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.