Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ea52d2

Browse files
committed
"Super Ugly Number"
1 parent 4419da2 commit 4ea52d2

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

‎README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
2525

2626
| | Problem | Solution |
2727
| --- | ------------------------------------------------------------ | ------------------ |
28+
| 315 | [Count of Smaller Numbers After Self] | |
29+
| 314 | [Binary Tree Vertical Order Traversal]| |
30+
| 313 | [Super Ugly Number] | [C](src/313.c) |
31+
| 312 | [Burst Balloons] | |
32+
| 311 | [Sparse Matrix Multiplication]| |
33+
| 310 | [Minimum Height Trees] | |
34+
| 309 | [Best Time to Buy and Sell Stock with Cooldown] | |
35+
| 308 | [Range Sum Query 2D - Mutable]| |
36+
| 307 | [Range Sum Query - Mutable] | |
37+
| 306 | [Additive Number] | |
38+
| 305 | [Number of Islands II]| |
39+
| 304 | [Range Sum Query 2D - Immutable] | |
40+
| 303 | [Range Sum Query - Immutable] | |
41+
| 302 | [Smallest Rectangle Enclosing Black Pixels]| |
42+
| 301 | [Remove Invalid Parentheses] | |
2843
| 300 | [Longest Increasing Subsequence] | [C](src/300.c) |
2944
| 299 | [Bulls and Cows] | [C](src/299.c) |
3045
| 298 | [Binary Tree Longest Consecutive Sequence]| |
@@ -313,6 +328,21 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
313328
[LeetCode algorithm problems]: https://leetcode.com/problemset/algorithms/
314329

315330

331+
[Count of Smaller Numbers After Self]: https://leetcode.com/problems/count-of-smaller-numbers-after-self/
332+
[Binary Tree Vertical Order Traversal]: https://leetcode.com/problems/binary-tree-vertical-order-traversal/
333+
[Super Ugly Number]: https://leetcode.com/problems/super-ugly-number/
334+
[Burst Balloons]: https://leetcode.com/problems/burst-balloons/
335+
[Sparse Matrix Multiplication]: https://leetcode.com/problems/sparse-matrix-multiplication/
336+
[Minimum Height Trees]: https://leetcode.com/problems/minimum-height-trees/
337+
[Best Time to Buy and Sell Stock with Cooldown]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
338+
[Range Sum Query 2D - Mutable]: https://leetcode.com/problems/range-sum-query-2d-mutable/
339+
[Range Sum Query - Mutable]: https://leetcode.com/problems/range-sum-query-mutable/
340+
[Additive Number]: https://leetcode.com/problems/additive-number/
341+
[Number of Islands II]: https://leetcode.com/problems/number-of-islands-ii/
342+
[Range Sum Query 2D - Immutable]: https://leetcode.com/problems/range-sum-query-2d-immutable/
343+
[Range Sum Query - Immutable]: https://leetcode.com/problems/range-sum-query-immutable/
344+
[Smallest Rectangle Enclosing Black Pixels]: https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/
345+
[Remove Invalid Parentheses]: https://leetcode.com/problems/remove-invalid-parentheses/
316346
[Longest Increasing Subsequence]: https://leetcode.com/problems/longest-increasing-subsequence/
317347
[Bulls and Cows]: https://leetcode.com/problems/bulls-and-cows/
318348
[Binary Tree Longest Consecutive Sequence]: https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/

‎src/313.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
#include <assert.h>
5+
6+
int nthSuperUglyNumber(int n, int* primes, int primesSize) {
7+
if (n <= 0 || primesSize <= 0) return 1;
8+
9+
int *index = (int *)calloc(primesSize, sizeof(int));
10+
int *nums = (int *)calloc(n, sizeof(int));
11+
12+
nums[0] = 1;
13+
int storageIndex = 1;
14+
15+
while (storageIndex < n) {
16+
int min = INT32_MAX;
17+
int i;
18+
for (i = 0; i < primesSize; i++) {
19+
if (nums[index[i]] * primes[i] < min) {
20+
min = nums[index[i]] * primes[i];
21+
}
22+
}
23+
nums[storageIndex++] = min;
24+
for (i = 0; i < primesSize; i++) {
25+
if (nums[index[i]] * primes[i] == min) {
26+
index[i]++;
27+
}
28+
}
29+
}
30+
31+
int ans = nums[n - 1];
32+
33+
free(index);
34+
free(nums);
35+
36+
return ans;
37+
}
38+
39+
int main() {
40+
int n = 3;
41+
int primes[] = { 2, 3, 5 };
42+
43+
assert(nthSuperUglyNumber(n, primes, sizeof(primes)/sizeof(primes[0])) == 3);
44+
45+
printf("all tests passed!\n");
46+
47+
return 0;
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /