264. ugly number II

Write a program to find the n-th ugly numbers.

Ugly contains only the number of prime factors is a positive integer of 2, 3, 5.

Example:

Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is a front number 10 ugly.
Description:  

1 is the number of ugly.
n is not more than 1,690.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/ugly-number-ii
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

Initially timeout with Violence

 1 class Solution {
 2     public boolean isUgly(int n)
 3     {
 4         if (n<=0)
 5             return false;
 6         while(n%2 ==0)
 7             n/=2;
 8         while(n%3 ==0)
 9             n/=3;
10         while(n%5 ==0)
11             n/=5;
12         return n==1;
13     }
14     public int nthUglyNumber(int n) {
15         int i=1;
16         int count=0;
17         while(count<n)
18         {
19             while(isUgly(i))
20             {
21                 count++;
22             }
23             i++;
24         }
25         return i-1;
26     }
27 }

After thought out how to optimize, looked at problem solution is found using dynamic programming + three hands, calculated prior to use to reduce the amount of calculation.

dp保存按序排列的丑数,三指针分别是*2,*3,*5,找出下一个丑数。

Refers to another person, "pro forma review of Flanders" explanation:

1. Using dynamic programming, the number is assumed that ugly looking subscript i corresponding DP [i], may take a number of all i several ugly before a number greater than 2 up ugly dp [i-1], this number is denoted is num1; ugly Similarly with all numbers before a number i by a number greater than 3 up ugly dp [i-1], this number is denoted num2; passenger before all numbers ugly until i is larger than number 5 on a ugly number dp [i-1], this number is denoted num3. This minimum number of three numbers is the number of i-th ugly dp [i].
2. But then, in fact, no need to count all the ugly before i take 2 or multiply by 3 or 5. ** before the number i is ugly, ugly must exist a number (labeled at index2), just larger than the subsequent ugly by 2 a number of i dp [i-1], the previous number index2 equal to less than 2 by ugly dp [i-1] **; we need only record index2, each index is assigned to the direct use of this number by the line 2, and updates the index does not satisfy the next timing. Similarly we have record by 3 by 5 and the corresponding index.

The first point is easy to understand, the second point means for 2 * pointer, only just greater than 2 after recording * dp [i-1] subscript i2, * 3, * 5 The same pointer. Such dp [I] is the num1, num2, num3 minimum value is multiplied by the value corresponding to the pointer, dp [i] is the value of dp [n-1] is.

public int nthUglyNumber(int n) {
        int[] dp = new int[n];
        dp[0] = 1;
        int i2 = 0, i3 = 0, i5 = 0;
        for (int i = 1; i < n; i++) {
            int min = Math.min(dp[i2] * 2, Math.min(dp[i3] * 3, dp[i5] * 5));
            if (min == dp[i2] * 2) i2++;
            if (min == dp[i3] * 3) i3++;
            if (min == dp[i5] * 5) i5++;
            dp[i] = min;
        }

        return dp[n - 1];
    }

Author: pphdsny
Links: HTTPS: // leetcode-cn.com/problems/ugly-number-ii/solution/javati-jie-dong-tai-gui-hua-san-zhi-zhen-by-pphdsn/ 
Source: stay button (LeetCode )
Copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/zccfrancis/p/12193510.html