Cattle off network - the number of ugly wins the office-

Title: 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.
Ideas: first thought is violent solution, a judgment is not a ugly number, and a counter with statistics is the first of several ugly number, but the time efficiency of the algorithm is very low, can not pass OJ.
Before the violent solution of the reason is low efficiency, largely because whether a number is not ugly number, we need to calculate it.
Therefore, we can first list the first few ugly number
1, 2, 3, 4, 6, 8, 9, 10, 12,
we can see that ugly ugly number should be multiplied by the previous number 2, 3 or 5 result. We can create an array, the number of storage sorted ugly, ugly number each time to calculate the next time, based on the number of ugly before multiplied by 2, 3, or 5, respectively, and selected larger than the current maximum number of ugly ugly number, the smallest one, continued into the array until it finds the index th ugly numbers.
In fact, no need to start from scratch each time multiplied by 2, 3 or 5, respectively, because the existing number of ugly is sequentially stored in the array, bound to a number of the present position t2, t2 before the number, multiplied by 2 less than the maximum number of ugly already, the number after t2, multiplied by two is greater than the existing number of ugly, so we only need to record the position every time t2, and continue to update. For 3 and 5, also the same t3 and t5.

class Solution {
public:
	int GetUglyNumber_Solution(int index) {
		if (index <= 0) return 0;
		vector<int> v;
		int t2 = 0, t3 = 0, t5 = 0;
		int m2, m3, m5;
		int nmax = 1;
		v.push_back(1);
		while (v.size() < index)
		{
			for (int i = t2; i < v.size(); ++i)
			{
				if ((v[i] * 2) > nmax)
				{
					m2 = v[i] * 2;
					t2 = i;
					break;
				}
			}
			for (int i = t3; i < v.size(); ++i)
			{
				if ((v[i] * 3) > nmax)
				{
					m3 = v[i] * 3;
					t3 = i;
					break;
				}
			}
			for (int i = t5; i < v.size(); ++i)
			{
				if ((v[i] * 5) > nmax)
				{
					m5 = v[i] * 5;
					t5 = i;
					break;
				}
			}
			nmax = min(m2, min(m3, m5));
			v.push_back(nmax);
		}
		return nmax;
	}
	
};

python's solution:

class Solution:
    def GetUglyNumber_Solution(self, index):
        if index <= 0:
            return 0
        t2 = 0
        t3 = 0
        t5 = 0
        nmax = 1
        v = [1]
        m2 = 0
        m3 = 0
        m5 = 0
        while len(v) < index:
            for i in range(t2, len(v)):
                if (v[i]*2) > nmax:
                    m2 = v[i]*2
                    t2 = i
                    break
            for i in range(t3, len(v)):
                if (v[i] * 3) > nmax:
                    m3 = v[i] * 3
                    t3 = i
                    break
            for i in range(t5, len(v)):
                if (v[i]*5) > nmax:
                    m5 = v[i]*5
                    t5 = i
                    break
            nmax = min(m2, m3, m5)
            v.append(nmax)
        return nmax

Guess you like

Origin blog.csdn.net/qq_43387999/article/details/90774203