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. 1 <= X <= 10^9
  2. 1 <= Y <= 10^9

Approach #1: Math. [Java]

class Solution {
public int brokenCalc(int x, int y) {
int count = 0;
while (y != x) {
if (x > y) return x - y + count; if (y % 2 != 0) y += 1;
else y /= 2; count++;
} return count;
}
}

  

Analysis:

First, let us see if the solution exists or not.

Clearly, we can keep doubling x till it goes beyond y. Then we can keep decreamenting x till it reaches y. Since the number of operations is not limited, so we conclude that a solution exists.

So now, consider an optimal solution (any solution with the minimal number of steps).

The path is nothing but a sequence of numbers that start at x and end at y.

Assume (x <= y). The other case is trivial

Case 1) Y is odd

Now, consider the last second element of the sequence (optimal path). Recall that we can only move in the sequence via the allowed moves (in the forward direction, we multiply by 2 or decreament by 1). Let us back track and see which move did we actually use to get to y. (obviously it has to be one of the two moves).

Now, the move could not have been multiplication by 2, or else y would have been a multiple of 2, which violates our assumption. So the only possible move is the decrement move. It means that the last second term of the sequence is indeed y + 1 if y is odd. (And there is no other possibility).

So now we just need to compute the optimal length to reach y + 1, and then add 1 to our answer to get the optimal path length for y. (Why? It happens because y + 1 lies in an optimal path and any subpath of the optimal path must be optimal or else it would violates our assumptions).

Case 2) Y is even. Say, y = 2m

First, let us sudy the worst case analysis of what is the maximum number that you would touch if you play optimally.

Clearly it is 2 * (y - 1), since in the worst case, you may end up starting at y - 1 and jumping to 2 * (y - 1) and then coming back. In all other cases, the jump will lead you to a number less than 2 * (y - 1) and you can easily come back to y one step at a time.

Let us denote 2 * ( y - 1 ) as jump_max.

Now, if y is even, we cannot say anything about the last second term of the sequence. (The move could be either multiplication or decrement).

However, let us see what happens if the last move was multiplication by 2.

Clearly, the last second element in this case is y / 2 = m

So we need to compute the optimal path length to reach m and then we can add 1 to our answer. (But this is valid only if we know that the last move was indeed multiplication.)

what if the last move was decrement?

In that case, the last second element become 2m + 1, (odd number), and by the 1st lemma, we conclude that the last thrid number is 2m + 2.

Now 2m + 2 is an even number so either a jump happens or it's descendant is 2m + 4. So we keep going to the rigth untill we find ak such that 2m + 2k is obtained be jumping from m+k. Clearly such a number exists as the largest number we can encounter is jump_max.

So, now the path looks like:

x .......(m + k) -> 2 (m + k) -> (2m + 2k - 2) -> ...... y

But, if you observe carefully, after we reach (m + k) we can decrement k times to reach m and then double to get y. This would cost us (k+1) operations + the cost to reach (m + k). However, the current cost is (1 + 2 (m + k) - 2m) = (2k + 1) operations + the cost to reach (m+k). Since the new cost is lower, this violates our assumption that the original sequence was an optimal path. Therefore we have a contradiction and we conclude that the last move could not have been decrement.

Conclusion:

If y is odd, we know that the last number to be reached before y is (y + 1) (in the optimal path)

If y is even, we know that the last number to be reached before y is y / 2 (in the optimal path)

So finally we have recursive relation.

if (x >= y)

cost(x, y) = x - y

if (x < y)

cost(x, y) = 1 + cost(x, y+ 1) if y is odd

cost(x, y) = 1 + cost(x, y / 2) if y is even

This analysis may be easy to understand:

Trying to prove that if Y is even, the last operation must be doubling:

hypothesis: there can be one or more decrement from Y + 1 to Y in the shortest path, where last bit of Y is 0

since last bit of Y + 1 is 1, it must be decrement from Y + 2 (doubling can never make an 1 on last bit)

two options at Y + 2:

decrement from Y + 3, it's the same as the starting point Y + 1 and Y:

doubling from (Y+2)/2 three moves used from (Y+2)/2 to Y: double to Y + 2, decrement to Y+1, decrement to Y, while there is a shorter path: decrement to Y / 2, double to Y.

there we get a contradiction to the hypothesis

so the hypothesis is false

hence, there can be none decrement move(s) from Y + 1 to Y in the shortest path is last bit of Y is 0.

Reference:

https://leetcode.com/problems/broken-calculator/discuss/236565/Detailed-Proof-Of-Correctness-Greedy-Algorithm

991. Broken Calculator的更多相关文章

  1. LC 991. Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  2. 【leetcode】991. Broken Calculator

    题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...

  3. 【LeetCode】991. Broken Calculator 解题报告(Python)

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

  4. [Swift]LeetCode991. 坏了的计算器 | Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  5. 123th LeetCode Weekly Contest Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  6. A Broken Calculator 最详细的解题报告

    题目来源:A Broken Calculator 题目如下(链接有可能无法访问): A Broken Calculator Time limit : 2sec / Stack limit : 256M ...

  7. 【LeetCode】Broken Calculator(坏了的计算器)

    这道题是LeetCode里的第991道题. 题目描述: 在显示着数字的坏计算器上,我们可以执行以下两种操作: 双倍(Double):将显示屏上的数字乘 2: 递减(Decrement):将显示屏上的数 ...

  8. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. Qstring和String的区别

    QString qTest; std::string sTest = qTest.toStdString(); qTest = QString::fromStdString(sTest); //进入两 ...

  2. Mybatis系列全解(三):Mybatis简单CRUD使用介绍

    封面:洛小汐 作者:潘潘 在理解中执行,在执行中理解,学习技术也循此道. 前言 上一篇文章 <Mybatis系列全解(二):Mybatis简介与环境搭建> ,我们对 Mybatis 做了初 ...

  3. Centos7网络配置——设置固定ip

    有段时间没有用虚拟机了,这几天翻出了以前的虚拟机,写几个demo,但遇到了一个让人难受的问题.使用xshell远程连接虚拟机,命令还没敲几个,不到一分钟就自动断开了,只能重新再次连接.啥事都干不成,有 ...

  4. Linux-mysql服务级别对DB的操作要领[导出-导入(执行SQL)]及修改数据库名称

    A:docker容器的mysql docker exec -it mysql bash -- 进入容器 备份脚本 mysqldump -uroot -p123456 --databases dbNam ...

  5. h5移动端常见的问题及解决方案

    01.ios端兼容input高度 #问题描述 input输入框光标,光标的高度和父盒子的高度一样,而android手机没问题 android ios #产生原因 通常我们习惯用height属性设置行间 ...

  6. 3.DataFrame的增删改查

    以此为例 一.DataFrame的初步认知 在pandas中完成数据读取后数据以DataFrame保存.在操作时要以DataFrame函数进行了解 函数 含义 示例 values 元素 index 索 ...

  7. PTA 数组循环右移

    6-2 数组循环右移 (20 分)   本题要求实现一个对数组进行循环右移的简单函数:一个数组a中存有n(>)个整数,将每个整数循环向右移m(≥)个位置,即将a中的数据由(a​0​​a​1​​⋯ ...

  8. CQGUI框架之阴影圆角窗口实现

    CQGUI框架之阴影圆角窗口实现 大家好,我是IT文艺男,来自一线大厂的一线程序员 今天给大家讲解基于C++/Qt的CQGUI框架的阴影圆角窗口实现,实现效果如下图所示:: CQGUI开发环境:: M ...

  9. 京东 vue3 组件库震撼升级,如约而至!

    京东零售开源项目 NutUI 是一套京东风格的轻量级移动端 Vue 组件库,是开发和服务于移动 Web 界面的企业级产品.经过长时间的开发与打磨,NutUI 3.0 终于和大家见面了!3.0 版本在技 ...

  10. Java例题_30 在已经排好序的数组中插入值

    1 /*30 [程序 30 插入数字] 2 题目:有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. 3 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插 ...