{"id":4831,"date":"2019-02-10T09:32:17","date_gmt":"2019-02-10T17:32:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4831"},"modified":"2019-02-10T09:33:20","modified_gmt":"2019-02-10T17:33:20","slug":"leetcode-991-broken-calculator","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-991-broken-calculator\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 991. Broken Calculator"},"content":{"rendered":"\n<p>On a broken calculator that has a number showing on its display, we can perform two operations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Double<\/strong>: Multiply the number on the display by 2, or;<\/li><li><strong>Decrement<\/strong>: Subtract 1 from the number on the display.<\/li><\/ul>\n\n\n\n<p>Initially, the calculator is displaying the number&nbsp;<code>X<\/code>.<\/p>\n\n\n\n<p>Return the minimum number of operations needed to display the number&nbsp;<code>Y<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>X = 2, Y = 3\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>Use double operation and then decrement operation {2 -&gt; 4 -&gt; 3}.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>X = 5, Y = 8\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>Use decrement and then double {5 -&gt; 4 -&gt; 8}.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>X = 3, Y = 10\n<strong>Output: <\/strong>3\n<strong>Explanation: <\/strong> Use double, decrement and double {3 -&gt; 6 -&gt; 5 -&gt; 10}.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>X = 1024, Y = 1\n<strong>Output: <\/strong>1023\n<strong>Explanation: <\/strong>Use decrement operations 1023 times.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= X &lt;= 10^9<\/code><\/li><li><code>1 &lt;= Y &lt;= 10^9<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Thinking backwards, making  Y &lt;= X by adding 1 or dividing 2.<\/p>\n\n\n\n<p>If Y is even, (Y + 1) \/\/ 2 == Y \/\/ 2, there is no need to do the extra step<br>If Y is odd (Y + 1) \/\/ 2  = (Y \/\/ 2) + 1, so only do + 1 when Y is odd<\/p>\n\n\n\n<p>Time complexity: O(log(Y-X))<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua, running time: 4 ms, 4.7 MB\nclass Solution {\npublic:\n  int brokenCalc(int X, int Y) {\n    if (X <= Y) return X - Y;\n    return 1 + brokenCalc(X, Y % 2 ? Y + 1 : Y \/ 2);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,31,177],"class_list":["post-4831","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4831"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4831\/revisions"}],"predecessor-version":[{"id":4834,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4831\/revisions\/4834"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}