Broken Calculator
Medium · Math, Greedy
There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:
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
Use double operation and then decrement operation {2 -> 4 -> 3}.
Example 2
Input: startValue = 5, target = 8 Output: 2
Use decrement and then double {5 -> 4 -> 8}.
Example 3
Input: startValue = 3, target = 10 Output: 3
Use double, decrement and double {3 -> 6 -> 5 -> 10}.
Constraints
- 1 <= startValue, target <= 109
Starter code (python)
class Solution(object):
def brokenCalc(self, startValue, target):
"""
:type startValue: int
:type target: int
:rtype: int
"""
Related problems
- Add Two Numbers (Medium)
- Reverse Integer (Medium)
- Palindrome Number (Easy)
Previous: Add to Array-Form of Integer · Next: Subarrays with K Different Integers · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.