【LeetCode】991. Broken Calculator 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/broken-calculator/
题目描述
On a broken calculator that has a number showing on its display, we can perform two operations:
- Double: Multiply the number on the display by 2, or;
- Decrement: Subtract 1 from the number on the display.
Initially, the calculator is displaying the number X.
Return the minimum number of operations needed to display the number Y.
Example 1:
Input: X = 2, Y = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
Example 2:
Input: X = 5, Y = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.
Example 3:
Input: X = 3, Y = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
Example 4:
Input: X = 1024, Y = 1
Output: 1023
Explanation: Use decrement operations 1023 times.
Note:
1 <= X <= 10^91 <= Y <= 10^9
题目大意
有一个坏掉了的计算器,只能对现在正在显示的数字进行两种操作:
- 翻倍
- 减1
已知初始化的时候计算器显示的数字是X,问最少需要多少步操作才能得到目标数字Y。
解题方法
这个题第一感觉肯定是BFS,但是很显然数字的取值范围太大,BFS会超时。
那么这个题就是个数学问题了……下面的内容摘自演员的自我修养:
首先我们发现x要么乘2要么减1,如果x初始就比y大,那么只能一直做减法!
在x小于y的情况下:
- 如果y是奇数,那么最后一个操作一定是减1(显然)
- 如果y是偶数呢?最后操作一定是乘2吗?答案是yes!
因为对于某个x现在要变为y可能是先乘2再做若干次减法,也可能是先做若干次减法再乘2
第一种用式子表示为1 + 2*x-y,第二种用式子表示为x-y/2 + 1
显然式子2的结果永远小于等于式子1的结果!
所以,我们想要最小化次数一定是先减法再乘2,也就是y为偶数时,最后的操作一定是乘2。
那么这里我们就从y开始反推,是奇数就加1,是偶数就除2,一直到y小于等于x为止!
python代码如下:
class Solution(object):
def brokenCalc(self, X, Y):
"""
:type X: int
:type Y: int
:rtype: int
"""
if X > Y: return X - Y
res = 0
while X < Y:
if Y % 2 == 1:
Y += 1
res += 1
Y //= 2
res += 1
return res + X - Y
日期
2019 年 2 月 21 日 —— 一放假就再难抓紧了
【LeetCode】991. Broken Calculator 解题报告(Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【leetcode】991. Broken Calculator
题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- SNP 过滤(一)
通用过滤 Vcftools(http://vcftools.sourceforge.net) 对vcf文件进行过滤 第一步:过滤最低质量低于30,次等位基因深度(minor allele count) ...
- MYSQL5.8----M2
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_s ...
- windows和linux文本的编码格式不一样所出的错
windows下编写的python脚本上传的linux下执行会出现错误: usr/bin/python^M: bad interpreter: No such file or directory 原因 ...
- MAC——解决问题:打不开,因为它来自身份不明的开发者
今天在mac电脑上,下载了一个软件,是从某个网页上下载的,点击却不能打开,弹出窗口提示说"打不开xx,因为它来自身份不明的开发者",怎么解决?下面来看下. 方法/步骤 点击 ...
- raid0 raid1 raid5
关于Raid0,Raid1,Raid5,Raid10的总结 RAID0 定义: RAID 0又称为Stripe或Striping,它代表了所有RAID级别中最高的存储性能.RAID 0提高存储性能 ...
- 振鹏学习Java的第二天!
一.今日收获 1.了解了eclipse的具体使用方法. 2.学习了Java程序设计完全手册的第一章内容,明白了相关知识. 3.通过看哔哩哔哩的java的教程视频了解了Dos命令及java的变量和常量. ...
- 一文搞懂指标采集利器 Telegraf
作者| 姜闻名 来源|尔达 Erda 公众号 导读:为了让大家更好的了解 MSP 中 APM 系统的设计实现,我们决定编写一个<详聊微服务观测>系列文章,深入 APM 系统的产品.架构 ...
- django中的filter(), all(), get()
1. 类名.objects中的get(), filter(), all() 的区别 结论: (1)all()返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使 ...
- Linux启动初始化配置文件
Linux启动初始化配置文件(1)/etc/profile 登录时,会执行. 全局(公有)配置,不管是哪个用户,登录时都会读取该文件. (2)/ect/bashrc Ubuntu没有此文件,与之对应的 ...
- javaAPI1
Iterable<T>接口, Iterator<T> iterator() Collection<E>:接口,add(E e) ,size() , Object[] ...