题目如下:

Write a program to find the n-th ugly number.

Ugly numbers are positive integers which are divisible by a or b or c.

Example 1:

Input: n = 3, a = 2, b = 3, c = 5
Output: 4
Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 3rd is 4.

Example 2:

Input: n = 4, a = 2, b = 3, c = 4
Output: 6
Explanation: The ugly numbers are 2, 3, 4, 6, 8, 9, 10, 12... The 4th is 6.

Example 3:

Input: n = 5, a = 2, b = 11, c = 13
Output: 10
Explanation: The ugly numbers are 2, 4, 6, 8, 10, 11, 12, 13... The 5th is 10.

Example 4:

Input: n = 1000000000, a = 2, b = 217983653, c = 336916467
Output: 1999999984

Constraints:

  • 1 <= n, a, b, c <= 10^9
  • 1 <= a * b * c <= 10^18
  • It's guaranteed that the result will be in range [1, 2 * 10^9]

解题思路:先看这么一个问题,怎么求出任意一个数x,在[1,x]区间内有几个丑数?只要(x/a)+(x/b)+(x/c)即可,但是可能会有重复的值,比如a=2,b=3时,丑数6就会多计算一次,所以还需要减去( x/lcm(a,b) + x/lcm(c,b)  + x/lcm(a,c) )。这里lcm(a,b)表示a和b的最小公倍数。这样是不是就好了呢?还有lcm(a,b,c)的情况,因为前面求两两最小公倍数的时候多减了一次,所以这里要再加上 x/lcm(a,b,c) ,这里就可以得到x前面有几个丑数,即为count。由于x不一定是丑数,所有只要求出小于x的最大丑数,这个丑数所在的位置就是count。由于丑数的数量随着x的增加而增加,所以用二分查找的方法很容易就可以求得指定位置的丑数。

代码如下:

class Solution(object):
def nthUglyNumber(self, n, a, b, c):
"""
:type n: int
:type a: int
:type b: int
:type c: int
:rtype: int
"""
def gcd(a, b):
return a if b == 0 else gcd(b, a % b)
def lcm(a, b):
return a * b // gcd(a, b)
def getCount(v,divisor):
return v / divisor
divisor_list = sorted([a,b,c])
if divisor_list[2] % divisor_list[0] == 0:
del divisor_list[2]
if divisor_list[1] % divisor_list[0] == 0:
del divisor_list[1]
if len(divisor_list) == 3 and divisor_list[2] % divisor_list[1] == 0:
del divisor_list[2] lcm_list = set()
for i in range(len(divisor_list)):
for j in range(len(divisor_list)):
if i != j:lcm_list.add(lcm(divisor_list[i],divisor_list[j]))
common_lcm = None
if len(divisor_list) == 3:
common_lcm = lcm(divisor_list[0],divisor_list[1])
common_lcm = lcm(common_lcm, divisor_list[2]) low ,high = 1, 2*(10**9)
val = 0
while low <= high:
#print low,high
mid = (low + high)/2
#mid = 120
#if mid == 128:
# pass
ugly_count = 0
for i in divisor_list:
ugly_count += getCount(mid,i)
for i in lcm_list:
ugly_count -= getCount(mid,i)
if common_lcm != None:
ugly_count += getCount(mid,common_lcm)
if n == ugly_count:
val = mid
break
elif n > ugly_count:
low = mid + 1
else:
high = mid - 1
res = []
for i in divisor_list:
res.append(val/i*i)
for i in lcm_list:
res.append(val / i * i)
return sorted(res)[-1]

【leetcode】1201. Ugly Number III的更多相关文章

  1. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

  2. 【LeetCode】263. Ugly Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...

  3. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  4. 【LeetCode】263. Ugly Number

    Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are posi ...

  5. 【Leetcode】264. Ugly Number II ,丑数

    原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...

  6. 【LeetCode】-- 260. Single Number III

    问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...

  7. 【LeetCode】260. Single Number III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 题目地址:https://leet ...

  8. 【leetcode】260. Single Number III

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  9. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

随机推荐

  1. mysql默认的存储引擎是什么?它们的区别有哪些?mysql中索引有哪些?

    1.mysql默认引擎 mysql-5.1版本之前默认引擎是MyISAM,之后是innoDB 2.关系 MyISAM是非集聚引擎,支持全文索引;不支持事务;它是表级锁;会保存表的具体行数. innoD ...

  2. echarts 饼图-->如何修改legend模板?

    首先需要在初始化图表的方法中过滤一下数据 ,将你需要的 名称  所占百分比 所占数量  筛选出来 let dataFilter = [ { value: 20, name: "未知" ...

  3. C#静态调用带有SoapHeader验证的WebServices

    转自:http://blog.csdn.net/u012995964/article/details/54562111 本文记录带有SoapHeader验证的WebServices服务创建.部署及C# ...

  4. yum安装nginx(Centos)

    测试人员需要了解Nginx?nginx的别名有很多:中间件,HTTP服务器,代理服务器等,这些名字都是作用的一个体现.在实际项目中,前后端分离,负载均衡等也是通过Nginx实现的,知己知彼,百战百胜. ...

  5. HDU 1029 Ignatius and the Princess IV (动态规划、思维)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  6. 小记---------linux远程连接集群内其他机器mysql库

    mysql -h -u maxwell -p#10.0.15.145 远程机器ip#-P 注意是大写P 端口#-u 用户#-p 密码

  7. 手写数字识别 卷积神经网络 Pytorch框架实现

    MNIST 手写数字识别 卷积神经网络 Pytorch框架 谨此纪念刚入门的我在卷积神经网络上面的摸爬滚打 说明 下面代码是使用pytorch来实现的LeNet,可以正常运行测试,自己添加了一些注释, ...

  8. java 线程池 - ThreadPoolExecutor

    1. 为什么要用线程池 减少资源的开销 减少了每次创建线程.销毁线程的开销. 提高响应速度 ,每次请求到来时,由于线程的创建已经完成,故可以直接执行任务,因此提高了响应速度. 提高线程的可管理性 ,线 ...

  9. 利用aopc创建schema失败

    执行neo4j-graph-algorithms的例子,运行以下代码报错: CALL apoc.schema.assert( {Category:['name']}, {Business:['id'] ...

  10. npm学习(四)之如何安装全局包、更新全局安装的包、卸载全局安装的包

    如何安装全局包 有两种方式用来安装 npm 包:本地安装和全局安装.选用哪种方式来安装,取决于你如何使用这个包. 如果你想将其作为一个命令行工具,那么你应该将其安装到全局.这种安装方式后可以让你在任何 ...