Clear (Albeit Slow) Python Solution


  • 0
    G
    def nthUglyNumber(self, n):
        if n == 1:
            return 1
            
        i = j = k = 1
        count = 1
        numList = [1]
        while count < n:
            nextUgly, ind = min( (2*numList[i-1], 'i'), (3*numList[j-1], 'j'), (5*numList[k-1], 'k'))
            if ind == 'i':
                i+=1
            elif ind == 'j':
                j+=1
            else:
                k+=1
            if nextUgly != numList[-1]:
                numList.append(nextUgly)
                count +=1
            
        return numList[-1]

Log in to reply
 

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