Class Solution

java.lang.Object
g0901_1000.s0991_broken_calculator.Solution

public class Solution extends Object
991 - Broken Calculator.<p>Medium</p> <p>There is a broken calculator that has the integer <code>startValue</code> on its display initially. In one operation, you can:</p> <ul> <li>multiply the number on display by <code>2</code>, or</li> <li>subtract <code>1</code> from the number on display.</li> </ul> <p>Given two integers <code>startValue</code> and <code>target</code>, return <em>the minimum number of operations needed to display</em> <code>target</code> <em>on the calculator</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> startValue = 2, target = 3</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> Use double operation and then decrement operation {2 -> 4 -> 3}.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> startValue = 5, target = 8</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> Use decrement and then double {5 -> 4 -> 8}.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> startValue = 3, target = 10</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Use double, decrement and double {3 -> 6 -> 5 -> 10}.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= x, y <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • brokenCalc

      public int brokenCalc(int startValue, int target)