【leetcode】1201. Ugly Number III
题目如下:
Write a program to find the
n-th ugly number.Ugly numbers are positive integers which are divisible by
aorborc.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: 1999999984Constraints:
1 <= n, a, b, c <= 10^91 <= 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的更多相关文章
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
- 【LeetCode】263. Ugly Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【LeetCode】263. Ugly Number
Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are posi ...
- 【Leetcode】264. Ugly Number II ,丑数
原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...
- 【LeetCode】-- 260. Single Number III
问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...
- 【LeetCode】260. Single Number III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 题目地址:https://leet ...
- 【leetcode】260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
随机推荐
- CTF—WEB—sql注入之宽字节注入
宽字节注入 宽字节注入是利用mysql的一个特性,mysql在使用GBK编码(GBK就是常说的宽字节之一,实际上只有两字节)的时候,会认为两个字符是一个汉字(前一个ascii码要大于128,才到汉字 ...
- 【PyTorch】计算局部相似矩阵
计算局部相似矩阵 代码文档:https://github.com/lartpang/mypython/blob/master/2019-09-25%E8%AE%A1%E7%AE%97%E5%B1%80 ...
- maven的各种异常
1. 问题 本文将讨论Spring中最常见的配置问题 —— Spring的一个命名空间的名称空间处理程序没有找到. 大多数情况下,是由于一个特定的Spring的jar没有配置在classpath下,让 ...
- 微信小程序开发(三)----- 云开发案例
1.发送请求 2.云函数中发送请求,例子request https://github.com/request/request-promise 创建云函数movielist,右键在终端打开,输入 ...
- 插座-网络问题-ESP8266
//ATK-ESP8266模块测试主函数,检查WIFI模块是否在线 void atk_8266_test(void) { ))//检查WIFI模块是否在线 { atk_8266_quit_trans( ...
- $Prufer$序列
\(Prufer\)序列 \(Prufer\)序列与树的相互转换: 树->\(Prufer\)序列 找到一个编号最小的叶子结点,把这个点删掉并且把跟他连着的那个点的编号加入\(Prufer\)序 ...
- django-xadmin使用
django-xadmin使用基础环境为: PS:如下环境如需升级python则先升级python,然后安装django python3.6.4安装: http://www.cnblogs.com/c ...
- [.net core]11.异常页
.net core中的异常页很重要 因为可以查看异常的堆栈信息, 请求的参数(如果有),cookie, http头 帮助我们快速的定位问题 .net core web app 默认开启了异常页,但是 ...
- 7.jQuery之显示与隐藏效果
这里用到三个函数方法:show() hide() toggle() 注意点是三个方法里面的两个参数的使用,前一个参数是时间,表示显示速度:后一个参数是回调函数,只有前面的动画执行完之后,回调函数 ...
- 解决jenkins的Console Output中文乱码
1.本地机器设置环境变量(设置后需要注销计算机才能生效) key: JAVA_TOOL_OPTIONS value:-Dfile.encoding=UTF- 2. 通过Jenkins全局设置的方式 ...