forked from selfboot/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* @Author: [email protected] | ||
* @Last Modified time: 2016-07-10 10:55:05 | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
/* Just according to: | ||
* https://discuss.leetcode.com/topic/31012/7-line-consice-o-kn-c-solution | ||
*/ | ||
int nthSuperUglyNumber(int n, vector<int>& primes) { | ||
vector<int> index(primes.size(), 0), ugly_numbers(n, INT_MAX); | ||
ugly_numbers[0] = 1; | ||
for(int i=1; i<n; i++){ | ||
for(int j=0; j<primes.size();j++){ | ||
ugly_numbers[i]=min(ugly_numbers[i], ugly_numbers[index[j]]*primes[j]); | ||
} | ||
for(int j=0; j<primes.size();j++){ | ||
index[j] += (ugly_numbers[i]==ugly_numbers[index[j]]*primes[j]); | ||
} | ||
} | ||
return ugly_numbers[n-1]; | ||
} | ||
}; | ||
|
||
/* | ||
2 | ||
[2,3,5] | ||
100 | ||
[2,3,5,7,11] | ||
5 | ||
[2,3,5,7,11,13,17,19] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# @Author: [email protected] | ||
# @Last Modified time: 2016-07-10 10:32:18 | ||
|
||
|
||
class Solution(object): | ||
""" Basic idea is same as ugly number II. | ||
New ugly number is generated by multiplying a prime with | ||
previous generated ugly number. | ||
One catch is need to remove duplicate. | ||
Make code shorter and faster according to: | ||
https://discuss.leetcode.com/topic/33103/fast-python-solution-based-on-solution-for-ugly-number-ii | ||
""" | ||
def nthSuperUglyNumber(self, n, primes): | ||
k = len(primes) | ||
ugly_numbers = [1] * n | ||
index_record = [-1] * k | ||
value_record = [1] * k | ||
|
||
ugly_index = 0 | ||
while ugly_index < n: | ||
min_val = min(value_record) | ||
ugly_numbers[ugly_index] = min_val | ||
|
||
for i in range(k): | ||
# Remove duplicate numbers here. | ||
if value_record[i] == min_val: | ||
index_record[i] += 1 | ||
value_record[i] = primes[i] * ugly_numbers[index_record[i]] | ||
|
||
ugly_index += 1 | ||
|
||
return ugly_numbers[n - 1] | ||
|
||
""" | ||
2 | ||
[2,3,5] | ||
100 | ||
[2,3,5,7,11] | ||
5 | ||
[2,3,5,7,11,13,17,19] | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters