Class Solution
java.lang.Object
g2101_2200.s2139_minimum_moves_to_reach_target_score.Solution
2139 - Minimum Moves to Reach Target Score.<p>Medium</p>
<p>You are playing a game with integers. You start with the integer <code>1</code> and you want to reach the integer <code>target</code>.</p>
<p>In one move, you can either:</p>
<ul>
<li><strong>Increment</strong> the current integer by one (i.e., <code>x = x + 1</code>).</li>
<li><strong>Double</strong> the current integer (i.e., <code>x = 2 * x</code>).</li>
</ul>
<p>You can use the <strong>increment</strong> operation <strong>any</strong> number of times, however, you can only use the <strong>double</strong> operation <strong>at most</strong> <code>maxDoubles</code> times.</p>
<p>Given the two integers <code>target</code> and <code>maxDoubles</code>, return <em>the minimum number of moves needed to reach</em> <code>target</code> <em>starting with</em> <code>1</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> target = 5, maxDoubles = 0</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> Keep incrementing by 1 until you reach target.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> target = 19, maxDoubles = 2</p>
<p><strong>Output:</strong> 7</p>
<p><strong>Explanation:</strong> Initially, x = 1</p>
<p>Increment 3 times so x = 4</p>
<p>Double once so x = 8</p>
<p>Increment once so x = 9</p>
<p>Double again so x = 18</p>
<p>Increment once so x = 19</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> target = 10, maxDoubles = 4</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> Initially, x = 1</p>
<p>Increment once so x = 2</p>
<p>Double once so x = 4</p>
<p>Increment once so x = 5</p>
<p>Double again so x = 10</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= target <= 10<sup>9</sup></code></li>
<li><code>0 <= maxDoubles <= 100</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minMoves
public int minMoves(int target, int maxDoubles)
-