• 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];
        }
    };
    
  • 相关阅读:
    java struts2入门学习实例--用户注册
    java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器
    struts2基本配置详解2
    struts2基本配置详解
    使用maven+eclipse搭建最简单的struts2的HelloWorld
    2013年总结
    linux shell 脚本攻略学习20--awk命令入门详解
    linux shell 脚本攻略学习19--sed命令详解
    linux shell 脚本攻略学习18--grep命令详解
    linux shell 脚本攻略学习17--正则表达式入门
  • 原文地址:https://www.cnblogs.com/wevolf/p/15151645.html
Copyright © 2020-2023  润新知