{"id":9435,"date":"2022-01-30T08:00:45","date_gmt":"2022-01-30T16:00:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9435"},"modified":"2022-01-30T08:03:07","modified_gmt":"2022-01-30T16:03:07","slug":"leetcode-2139-minimum-moves-to-reach-target-score","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2139-minimum-moves-to-reach-target-score\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2139. Minimum Moves to Reach Target Score"},"content":{"rendered":"\n<p>You are playing a game with integers. You start with the integer&nbsp;<code>1<\/code>&nbsp;and you want to reach the integer&nbsp;<code>target<\/code>.<\/p>\n\n\n\n<p>In one move, you can either:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Increment<\/strong>&nbsp;the current integer by one (i.e.,&nbsp;<code>x = x + 1<\/code>).<\/li><li><strong>Double<\/strong>&nbsp;the current integer (i.e.,&nbsp;<code>x = 2 * x<\/code>).<\/li><\/ul>\n\n\n\n<p>You can use the&nbsp;<strong>increment<\/strong>&nbsp;operation&nbsp;<strong>any<\/strong>&nbsp;number of times, however, you can only use the&nbsp;<strong>double<\/strong>&nbsp;operation&nbsp;<strong>at most<\/strong>&nbsp;<code>maxDoubles<\/code>&nbsp;times.<\/p>\n\n\n\n<p>Given the two integers&nbsp;<code>target<\/code>&nbsp;and&nbsp;<code>maxDoubles<\/code>, return&nbsp;<em>the minimum number of moves needed to reach&nbsp;<\/em><code>target<\/code><em>&nbsp;starting with&nbsp;<\/em><code>1<\/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> target = 5, maxDoubles = 0\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> Keep incrementing by 1 until you reach target.\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> target = 19, maxDoubles = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> Initially, x = 1\nIncrement 3 times so x = 4\nDouble once so x = 8\nIncrement once so x = 9\nDouble again so x = 18\nIncrement once so x = 19\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> target = 10, maxDoubles = 4\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>Initially, x = 1\nIncrement once so x = 2\nDouble once so x = 4\nIncrement once so x = 5\nDouble again so x = 10\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= target &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>0 &lt;= maxDoubles &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Reverse + Greedy<\/strong><\/h2>\n\n\n\n<p>If num is odd, decrement it by 1. Divide num by 2 until maxdoubles times. Apply decrementing until 1 reached.<\/p>\n\n\n\n<p>ex1: 19 (dec)-> 18 (div1)-> 9 (dec) -> 8 (div2)-> 4 <meta charset=\"utf-8\">(dec)-> 3 <meta charset=\"utf-8\">(dec)-> 2 <meta charset=\"utf-8\">(dec)-> 1<\/p>\n\n\n\n<p>Time complexity: O(logn)<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\nclass Solution {\npublic:\n  int minMoves(int target, int maxDoubles) {\n    int ans = 0;\n    while (maxDoubles-- && target != 1) {\n      if (target & 1)\n        --target, ++ans;\n      ++ans;\n      target >>= 1;      \n    }\n    ans += (target - 1);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are playing a game with integers. You start with the integer&nbsp;1&nbsp;and you want to reach the integer&nbsp;target. In one move, you can either: Increment&nbsp;the&#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,246],"class_list":["post-9435","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-math","tag-medium","tag-reverse","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9435","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=9435"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9435\/revisions"}],"predecessor-version":[{"id":9439,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9435\/revisions\/9439"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}