New Problem Solution - "Super Ugly Number"
This commit is contained in:
parent
94e78a875d
commit
fe2be9918b
@ -63,6 +63,7 @@ LeetCode
|
||||
|321|[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/) | [C++](./algorithms/cpp/createMaximumNumber/CreateMaximumNumber.cpp)|Hard|
|
||||
|319|[Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) | [C++](./algorithms/cpp/bulbSwitcher/bulbSwitcher.cpp)|Medium|
|
||||
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [C++](./algorithms/cpp/countOfSmallerNumbersAfterSelf/countOfSmallerNumbersAfterSelf.cpp)|Hard|
|
||||
|313|[Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [C++](./algorithms/cpp/superUglyNumber/SuperUglyNumber.cpp)|Medium|
|
||||
|312|[Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [C++](./algorithms/cpp/burstBalloons/BurstBalloons.cpp)|Hard|
|
||||
|310|[Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [C++](./algorithms/cpp/minimumHeightTrees/MinimumHeightTrees.cpp)|Medium|
|
||||
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [C++](./algorithms/cpp/rangeSumQuery-Immutable/rangeSumQuery-Mutable/RangeSumQueryMutable.cpp)|Medium|
|
||||
|
69
algorithms/cpp/superUglyNumber/SuperUglyNumber.cpp
Normal file
69
algorithms/cpp/superUglyNumber/SuperUglyNumber.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
// Source : https://leetcode.com/problems/super-ugly-number/
|
||||
// Author : Hao Chen
|
||||
// Date : 2017-01-02
|
||||
|
||||
/***************************************************************************************
|
||||
*
|
||||
* Write a program to find the nth super ugly number.
|
||||
*
|
||||
* Super ugly numbers are positive numbers whose all prime factors are in the given
|
||||
* prime list
|
||||
* primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]
|
||||
* is the sequence of the first 12 super ugly numbers given primes
|
||||
* = [2, 7, 13, 19] of size 4.
|
||||
*
|
||||
* Note:
|
||||
* (1) 1 is a super ugly number for any given primes.
|
||||
* (2) The given numbers in primes are in ascending order.
|
||||
* (3) 0 k ≤ 100, 0 n ≤ 106, 0 primes[i]
|
||||
*
|
||||
* Credits:Special thanks to @dietpepsi for adding this problem and creating all test
|
||||
* cases.
|
||||
***************************************************************************************/
|
||||
|
||||
// As the solution we have for the ugly number II problem
|
||||
//
|
||||
// int nthUglyNumber(int n) {
|
||||
//
|
||||
// int i=0, j=0, k=0;
|
||||
// vector<int> ugly(1,1);
|
||||
//
|
||||
// while(ugly.size() < n){
|
||||
// int next = min(ugly[i]*2, ugly[j]*3, ugly[k]*5);
|
||||
// if (next == ugly[i]*2) i++;
|
||||
// if (next == ugly[j]*3) j++;
|
||||
// if (next == ugly[k]*5) k++;
|
||||
// ugly.push_back(next);
|
||||
// }
|
||||
// return ugly.back();
|
||||
// }
|
||||
//
|
||||
// The logic of solution is exacly same for both., except that instead of 3 numbers you have k numbers to consider.
|
||||
//
|
||||
//
|
||||
//
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
int nthSuperUglyNumber(int n, vector<int>& primes) {
|
||||
vector<int> ugly(1, 1);
|
||||
int len = primes.size();
|
||||
vector<int> pos(len, 0);
|
||||
|
||||
while( ugly.size() < n ) {
|
||||
int next = INT_MAX;
|
||||
for(int i=0; i<len; i++) {
|
||||
next = min(next, ugly[pos[i]] * primes[i]);
|
||||
}
|
||||
for(int i=0; i<len; i++) {
|
||||
if (next == ugly[pos[i]] * primes[i]) {
|
||||
pos[i]++;
|
||||
}
|
||||
}
|
||||
ugly.push_back(next);
|
||||
}
|
||||
return ugly.back();
|
||||
}
|
||||
|
||||
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user