Skip to content

Commit fe2be99

Browse files
committed
New Problem Solution - "Super Ugly Number"
1 parent 94e78a8 commit fe2be99

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ LeetCode
6363
|321|[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/) | [C++](./algorithms/cpp/createMaximumNumber/CreateMaximumNumber.cpp)|Hard|
6464
|319|[Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) | [C++](./algorithms/cpp/bulbSwitcher/bulbSwitcher.cpp)|Medium|
6565
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [C++](./algorithms/cpp/countOfSmallerNumbersAfterSelf/countOfSmallerNumbersAfterSelf.cpp)|Hard|
66+
|313|[Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [C++](./algorithms/cpp/superUglyNumber/SuperUglyNumber.cpp)|Medium|
6667
|312|[Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [C++](./algorithms/cpp/burstBalloons/BurstBalloons.cpp)|Hard|
6768
|310|[Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [C++](./algorithms/cpp/minimumHeightTrees/MinimumHeightTrees.cpp)|Medium|
6869
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [C++](./algorithms/cpp/rangeSumQuery-Immutable/rangeSumQuery-Mutable/RangeSumQueryMutable.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Source : https://leetcode.com/problems/super-ugly-number/
2+
// Author : Hao Chen
3+
// Date : 2017-01-02
4+
5+
/***************************************************************************************
6+
*
7+
* Write a program to find the nth super ugly number.
8+
*
9+
* Super ugly numbers are positive numbers whose all prime factors are in the given
10+
* prime list
11+
* primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]
12+
* is the sequence of the first 12 super ugly numbers given primes
13+
* = [2, 7, 13, 19] of size 4.
14+
*
15+
* Note:
16+
* (1) 1 is a super ugly number for any given primes.
17+
* (2) The given numbers in primes are in ascending order.
18+
* (3) 0 k ≤ 100, 0 n ≤ 106, 0 primes[i]
19+
*
20+
* Credits:Special thanks to @dietpepsi for adding this problem and creating all test
21+
* cases.
22+
***************************************************************************************/
23+
24+
// As the solution we have for the ugly number II problem
25+
//
26+
// int nthUglyNumber(int n) {
27+
//
28+
// int i=0, j=0, k=0;
29+
// vector<int> ugly(1,1);
30+
//
31+
// while(ugly.size() < n){
32+
// int next = min(ugly[i]*2, ugly[j]*3, ugly[k]*5);
33+
// if (next == ugly[i]*2) i++;
34+
// if (next == ugly[j]*3) j++;
35+
// if (next == ugly[k]*5) k++;
36+
// ugly.push_back(next);
37+
// }
38+
// return ugly.back();
39+
// }
40+
//
41+
// The logic of solution is exacly same for both., except that instead of 3 numbers you have k numbers to consider.
42+
//
43+
//
44+
//
45+
class Solution {
46+
47+
public:
48+
int nthSuperUglyNumber(int n, vector<int>& primes) {
49+
vector<int> ugly(1, 1);
50+
int len = primes.size();
51+
vector<int> pos(len, 0);
52+
53+
while( ugly.size() < n ) {
54+
int next = INT_MAX;
55+
for(int i=0; i<len; i++) {
56+
next = min(next, ugly[pos[i]] * primes[i]);
57+
}
58+
for(int i=0; i<len; i++) {
59+
if (next == ugly[pos[i]] * primes[i]) {
60+
pos[i]++;
61+
}
62+
}
63+
ugly.push_back(next);
64+
}
65+
return ugly.back();
66+
}
67+
68+
69+
};

0 commit comments

Comments
 (0)