Advertisement

Broken Calculator - LeetCode 991 Solution

Broken Calculator - Complete Solution Guide

Broken Calculator is LeetCode problem 991, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There is a broken calculator that has the integer startValue on its display initially. In one operation, you can: multiply the number on display by 2 , or subtract 1 from the number on display. Given two integers startValue and target , return the minimum number of operations needed to display target on the calculator . Example 1: Input: startValue = 2, target = 3 Output: 2 Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}. Example 2: Input: startValue = 5, target = 8

Detailed Explanation

The problem presents a broken calculator with only two operations: doubling the displayed number or subtracting one. Given an initial value `startValue` and a target value `target`, the goal is to determine the minimum number of operations required to transform `startValue` into `target` using only these two operations. The constraints specify that both `startValue` and `target` are positive integers between 1 and 10^9.

Solution Approach

The provided solution employs a greedy approach by working backward from the `target` to the `startValue`. It iteratively applies reverse operations: if the `target` is even, it divides it by 2 (undoing the doubling); otherwise, it adds 1 to the `target` (undoing the subtraction). This process continues until the `target` becomes less than or equal to the `startValue`. Finally, it adds the difference between `startValue` and `target` to the operation count to account for the remaining subtractions needed to reach the `startValue` from `target` (if `target` is less than `startValue` at the end).

Step-by-Step Algorithm

  1. Step 1: Initialize a variable `operations` to 0. This variable will store the count of operations.
  2. Step 2: Enter a `while` loop that continues as long as `target` is greater than `startValue`.
  3. Step 3: Inside the loop, increment `operations` by 1 for each operation performed.
  4. Step 4: Check if `target` is even (i.e., `target % 2 == 0`).
  5. Step 5: If `target` is even, divide it by 2 (`target //= 2`). This simulates reversing the doubling operation.
  6. Step 6: If `target` is odd, add 1 to it (`target += 1`). This simulates reversing the subtraction operation.
  7. Step 7: After the loop finishes (when `target` is no longer greater than `startValue`), add the difference `startValue - target` to `operations`. This accounts for the number of subtraction operations needed to reach `startValue` from the remaining `target` if target is less than startValue. Note this will be zero if target is startValue after the loop.
  8. Step 8: Return the final value of `operations`.

Key Insights

  • Insight 1: Working backward from the target is more efficient. Instead of trying to reach the target from the start, we can reverse the operations: divide the target by 2 (if even) or add 1 to the target.
  • Insight 2: Since startValue and target are positive, we can deduce that when target is greater than startValue, we should try to reach to target from the startValue. Doubling results in a larger number and subtracting reduces to a smaller number. If startValue is greater than target then the only way to reach target is by subtracting 1.
  • Insight 3: The optimal strategy involves prioritizing doubling operations when possible, which translates to halving when working backward. We only add 1 when the target is odd, as halving can only be applied to even numbers.

Complexity Analysis

Time Complexity: O(log(target))

Space Complexity: O(1)

Topics

This problem involves: Math, Greedy.

Companies

Asked at: Millennium, Nutanix.