Old Wei wins the offer to take you to learn --- Brush title series (arranged in an array 33. The smallest number)

33. The array arranged in the smallest number

problem:

It contains only the number of prime factors 2, 3 and 5 is referred to as the number of ugly (Ugly Number). E.g. 6,8 are several ugly, but not 14, because it contains seven prime factors. Traditionally we have 1 as the first ugly number. Seeking ascending through N ugly large number sequence.

solve:

thought:

Easy to understand explanation:
First, from the definition of the ugly we know, only a number of factors 2,3,5 ugly, so ugly number p = 2 ^ x * 3 ^ y * 5 ^ z, in other words a number ugly ugly constant multiplied by another number is multiplied by 2 or 3 or multiplied by 5, then we start from a multiply 2,3,5, 2,3,5 get ugly number three, from three in multiplied by the number of ugly departure 2,3,5 4,6,10,6,9,15,10,15,25 get ugly number nine, we found that the number of ugly *** this way to get repetitive, but we Title requires the N-th number of ugly, ugly number of such methods are obtained disorder. Then we can maintain a three queues:
(1) the number of array ugly: 1
by multiplying the cohort 2: 2
queue multiplied by 3: 3
by 5 queues: 5
selects the smallest of the three ugly queue head number of the array was added 2 while the minimum number is multiplied by three queues into 2,3,5;
(2) of the array ugly: 1,2
multiplied by the queue 2: 4
queues multiplied by 3: 3,6
multiplied by 5 queue: 5,10
select three queues smallest number 3 added ugly head of the array, while the minimum number is multiplied by three queues into 2,3,5;
(3) the number of array ugly: 1,2,3
queue multiplied by 2: 4,6
multiplying the cohort 3: 6,9
multiplying the cohort 5: 10, 15
selecting a minimum number of three queue ahead of the array 4 was added ugly, while the minimum number of 2,3,5 multiplied into three queues;
(4) the number of array ugly: 1,2,3,4
multiplied queue 2: 6,8
multiplied by the queue 3: 6, 9
times 5 the queue: 5,10,15,20
Selecting a minimum of three queues ugly added ahead of the array number 5, while the minimum number is multiplied by three queues into 2,3,5;
(5) the number of array ugly: 1,2,3,4,5
queue multiplied by 2: 6,8,10,
multiplied by three queues: 6,9,12,15
queue multiplied by 5: 10,15,20,25
selecting a minimum number of three queues in advance was added 6 ugly of the array, but we found that there are two queues head 6, so we pop two head of the queue, while 12,18,30 into three queues;
........................
questions:
1. Why three queue?
Number of ugly array of number must be orderly, because we are from a few ugly array of 2,3,5 multiplied by the minimum number of elected must not multiplied by 2, 3, than in the past, at the same time for three internal queue, multiplied by 2, 3, were placed in chronological order, so the same internal queue is orderly;
2. Why is the minimum number of three queues relatively ugly head into the number of arrays?
Because three is ordered queue, the head is extracted three smallest, is equivalent to the number of all three queues to find the smallest.
Realization of ideas:
we have no need to maintain three queues, only recorded three pointers indicate which step to reach; "|" represents a pointer, arr ugly represent the number of arrays;
(1) 1
| 2
| 3
| 5
currently pointer to 0,0 , 0, queue head ARR [0] * 2 = 2, ARR [0] = *. 3. 3, ARR [0] * =. 5. 5
(2). 1 2
2 |. 4
|. 3. 6
|. 5 10
1,0,0 current pointer to the queue head ARR [. 1] * = 2. 4, ARR [0] = *. 3. 3, ARR [0] =. 5. 5 *
(. 3). 3. 1 2
2 |. 6. 4
. 3 |. 6 . 9
|. 5 10 15
current pointer points 1,1,0, head of the queue arr [1] * 2 = 4 , arr [1] * 3 = 6, arr [0] * 5 = 5

python code:

# -*- coding:utf-8 -*-
class Solution:
    def GetUglyNumber_Solution(self, index):
        # write code here
        
        if(index<=0):
            return 0
        ugly_num=[1]
        index_first=0
        index_third=0
        index_five=0
        
        for _ in range(index-1):
            newugly=min(ugly_num[index_first]*2,ugly_num[index_third]*3,ugly_num[index_five]*5)
            ugly_num.append(newugly)
            if(newugly%2==0):
                index_first+=1
            if(newugly%3==0):
                index_third+=1
            if(newugly%5==0):
                index_five+=1
                
        return ugly_num[-1]
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/104988875