zoukankan      html  css  js  c++  java
  • Leetcode Ugly Number Solution

    Leetcode Ugly Number

    The Problem Ugly Number II and Super Ugly Number , because I didn't write the Ugly Number I, so, I don't have any idea at first. According to the related tag, I have an idea of Priority Queue and Hash Table, but I still want to know how to deal it with Dynamic Programming.

    For Dynamic Programming problem, I think the most important details is:

    1. Design an Array(usually) to save the Optimal solution, I mean the Optimal solution of subproblem is very important. A good design cloud make problem must easier, for example, in problem Best Time to Buy and Sell Stock IV
    2. In this problem, we can easily design the DP array, but State transition equation is difficult in this problem.
    1. We can easily define the DP[i] as the i-th Ugly number for provided primes array, so, How can we do the State transition from DP[i] to DP[i+1].
    2. Assume that primes as an array {5,7,13,19,23,37}.
    3. We find that, sometimes, DP[i+1] not only from DP[i], but it is from, DP[0]... ... DP[i].
    4. In this problem, firstly, DP[i+1] is bigger than DP[i], and that we can get DP[i+1] by plus numbers in array {5,7,13,19,23,37} with a unknown number, because all numbers in DP is Ugly numbers, and we can get a new Ugly number by plus a number in primes array with an Ugly number in DP.
    5. And we assume that there are six numbers(in this example), we represented as tokens[0] to tokens[5], these tokens will plus primes[0] to primes[5] respectively,
    6. We set the tokens[0] to tokens[5] as 1 at first, and then we can get the six Ugly numbers with {tokens[0]*primes[0], tokens[1]*primes[1], ...,... tokens[5]*primes[5]},
    7. Now, we should remember that, we need only six numbers (tokens[0] to tokens[5]) to get DP[i+1], So, the next step is change a number in tokens, and the tokens also are Ugly numbers, there are in DP array.
    8. we use Pointer[] array to represent index of tokens number in DP, this means that DP[Pointer[0]] = tokens[0], because tokens are in DP, so when we change the tokens, actually, we can only change it into other numbers in DP,
    9. We know that we get new Ugly number as Minimum number in {tokens[0]*primes[0], tokens[1]*primes[1], ...,... tokens[5]*primes[5]}, if it is tokens[3]*primes[3], then we should change tokens[3], how should we change the tokens[3], remember that tokens are in DP, and its index is Pointer, this means , DP[Pointer[3]] = tokens[3], we want change tokens[3], we can only change Pointer[3],
    10. Because we want to get bigger Ugly number, so , the tokens[3] must be bigger, so, it will be the next Ugly number in DP, its index will increase, so, Pointer[3] will change to Pointer[3]+1;
    11. After we change the tokens, we can get new Ugly number with tokens and primes
    class Solution {
    public:
        int nthSuperUglyNumber(int n, vector<int>& primes) {
            if(n == 1) {
                return 1;
            }
            int length = primes.size();
            vector<int> Pointer(length, 0);
            vector<int> DP(n, 0);
            int count = 1, last = 0;
            DP[0] = 1;
            while (count < n) {
                int temp = INT_MAX;
                for(int i = 0; i<length; i++) {
                    temp = min(temp, DP[Pointer[i]] * primes[i]);
                }
                for(int i = 0; i<length; i++) {
                    if(temp == DP[Pointer[i]] * primes[i]) {
                        Pointer[i] += 1;
                    }
                }
                DP[count] = temp;
                count ++ ;
            }
            return DP[n-1];
        }
    };
    
  • 相关阅读:
    Leetcode题库——7.反转整数
    (tomcat)tomcat启动过慢
    (tomcat)查看tomcat安装路径
    (JDK)cmd中只能执行java不能执行javac命令
    (课)学习进度报告二
    (数据导入)csv文件数据导入数据库
    (编码转换)转换文件编码
    (python开发)用cmd下载Python的第三方库所遇问题及解决方法
    (课)学习进度报告一
    (课)淘宝网质量属性场景
  • 原文地址:https://www.cnblogs.com/wevolf/p/15151645.html
Copyright © 2011-2022 走看看