mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<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> </p>
 | 
						|
<p><strong>Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> startValue = 2, target = 3
 | 
						|
<strong>Output:</strong> 2
 | 
						|
<strong>Explanation:</strong> Use double operation and then decrement operation {2 -> 4 -> 3}.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> startValue = 5, target = 8
 | 
						|
<strong>Output:</strong> 2
 | 
						|
<strong>Explanation:</strong> Use decrement and then double {5 -> 4 -> 8}.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 3:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> startValue = 3, target = 10
 | 
						|
<strong>Output:</strong> 3
 | 
						|
<strong>Explanation:</strong> Use double, decrement and double {3 -> 6 -> 5 -> 10}.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= x, y <= 10<sup>9</sup></code></li>
 | 
						|
</ul>
 |