【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 ...
随机推荐
- cd-hit 去除冗余序列
最近一篇NG中使用到的软件,用来去除冗余的contigs,现简单记录. CD-HIT早先是一个蛋白聚类的软件,其主要的特定就是快!(ps:不是所有快的都是好的) 其去除冗余序列的大概思路就是: 首先对 ...
- Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段
0.distinct用法 在oracle中distinct的使用主要是在查询中去除重复出现的数据 直接在字段前加distinct关键字即可,如:select distinct 名字 from tabl ...
- Oracle-trunc函数、round 函数、ceil函数和floor函数---处理数字函数使用
0.round函数 按照指定小数位数进行四舍五入运算. SELECT ROUND( number, [ decimal_places ] ) FROM DUAL #number : 待处理数值 de ...
- EXCEL-REPLACE()替换字符串最后几位 删除字符串最后几位
字符串 0M5(烈焰红) 我要删除最后一个字符")" 公式=REPLACE(ASC(字符串),LEN(ASC(字符串)),1,"") 解释:=REPLAC ...
- session与cookie 浏览器关闭时的区别
session与cookie 浏览器关闭时的区别 cookie是存储在本地,当cookie在浏览器关闭的时候,再次打开是否记录之前的值,这跟cookie的过期时间设置有关. 如果cookie的过期时间 ...
- 第三个基础框架 — springMVC — 更新完毕
1.什么是springMVC? 还是老规矩,百度百科一下 这里面说了一堆废话,去官网瞄一下 官网网址:https://docs.spring.io/spring-framework/docs/curr ...
- javaSE中级篇3——集合体系(另外一种存储容器)——更新完毕
集合还是一种工具,所以它们的包都在java.util包下 1.集合的整个体系结构(是需要掌握的体系,完全体系不是这样) 对图中所说的 序和重复 这两词的说明: 序:指的是添加进去的元素和取出来的元素 ...
- Factorization
Factorization or factoring consists of writing a number or another mathematical object as a product ...
- acquire, acre, across
acquire An acquired taste is an appreciation [鉴赏] for something unlikely to be enjoyed by a person w ...
- 【leetcode】222. Count Complete Tree Nodes(完全二叉树)
Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...