Skip to content

Commit

Permalink
add 313. Super Ugly Number
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed Jul 10, 2016
1 parent 7043957 commit a2d57eb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
33 changes: 33 additions & 0 deletions DynamicProgramming/313_SuperUglyNumber.cpp
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]
*/
45 changes: 45 additions & 0 deletions DynamicProgramming/313_SuperUglyNumber.py
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]
"""
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
* 303. [Range Sum Query - Immutable](DynamicProgramming/303_RangeSumQueryImmutable.py)
* 304. [Range Sum Query 2D - Immutable](DynamicProgramming/304_RangeSumQuery2DImmutable.py)
* 309. [Best Time to Buy and Sell Stock with Cooldown](DynamicProgramming/309_BestTimeBuySellStockWithCooldown.py)
* 313. [Super Ugly Number](DynamicProgramming/313_SuperUglyNumber.py)
* 337. [House Robber III](DynamicProgramming/337_HouseRobberIII.py)
* 368. [Largest Divisible Subset](DynamicProgramming/368_LargestDivisibleSubset.py)
* 357. [Count Numbers with Unique Digits](DynamicProgramming/357_CountNumbersWithUniqueDigits.py)
Expand Down

0 comments on commit a2d57eb

Please sign in to comment.