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:
- 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
- In this problem, we can easily design the DP array, but State transition equation is difficult in this problem.
- 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].
- Assume that primes as an array {5,7,13,19,23,37}.
- We find that, sometimes, DP[i+1] not only from DP[i], but it is from, DP[0]... ... DP[i].
- 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.
- 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,
- 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]},
- 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.
- 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,
- 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],
- 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;
- 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];
}
};