Integer Replacement - Complete Solution Guide
Integer Replacement is LeetCode problem 397, 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
Given a positive integer n , you can apply one of the following operations: If n is even, replace n with n / 2 . If n is odd, replace n with either n + 1 or n - 1 . Return the minimum number of operations needed for n to become 1 . Example 1: Input: n = 8 Output: 3 Explanation: 8 -> 4 -> 2 -> 1 Example 2: Input: n = 7 Output: 4 Explanation: 7 -> 8 -> 4 -> 2 -> 1 or 7 -> 6 -> 3 -> 2 -> 1 Example 3: Input: n = 4 Output: 2 Constraints: 1 <= n <= 2 31 - 1
Detailed Explanation
The problem requires you to find the minimum number of operations to transform a given positive integer 'n' to 1. The allowed operations are: if 'n' is even, divide it by 2; if 'n' is odd, either add 1 or subtract 1 from it. The input is a positive integer 'n' and the output is the minimum number of operations required.
Solution Approach
The provided code uses a `while` loop to repeatedly apply the operations until 'n' becomes 1. Inside the loop, it first checks if 'n' is even. If it is, it divides 'n' by 2. If 'n' is odd, it checks if 'n' is equal to 3. If it is, it adds 2 to the count and breaks the loop. Otherwise, if the last two bits of 'n' are equal to 01 (n&3 == 1), then it decrements 'n'. If the last two bits of 'n' are not equal to 01 (n&3 != 1), it increments 'n'. The operations are guided by the idea that we want to produce even number after the addition or subtraction. The count of operations is incremented in each iteration.
Step-by-Step Algorithm
- Step 1: Initialize a counter 'count' to 0. This will store the number of operations.
- Step 2: Start a `while` loop that continues as long as 'n' is greater than 1.
- Step 3: Inside the loop, check if 'n' is even using the bitwise AND operator (`n & 1 == 0`).
- Step 4: If 'n' is even, right-shift 'n' by 1 (equivalent to dividing by 2) using `n >>= 1`.
- Step 5: If 'n' is odd, check if 'n' is equal to 3. If so, increment count by 2 and break from the loop. Note: this must come before the next conditional.
- Step 6: If 'n' is odd and not equal to 3, use bitwise AND to check the last two bits of 'n' (`n & 3`).
- Step 7: If the last two bits are 01 (n & 3 == 1), decrement 'n' by 1.
- Step 8: Otherwise (if the last two bits are 11), increment 'n' by 1.
- Step 9: Increment 'count' by 1 to reflect the operation performed.
- Step 10: After the loop finishes, return the 'count'.
Key Insights
- Insight 1: The core idea is to repeatedly apply the given operations until 'n' becomes 1.
- Insight 2: When 'n' is odd, the choice between adding 1 and subtracting 1 is crucial. It's generally better to choose the operation that leads to more factors of 2 in the subsequent steps, allowing more efficient divisions.
- Insight 3: The special case when n=3 needs explicit handling. It's better to transform 3 -> 2 -> 1 in 2 steps, instead of 3 -> 4 -> 2 -> 1 which requires 3 steps.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(1)
Topics
This problem involves: Dynamic Programming, Greedy, Bit Manipulation, Memoization.
Companies
Asked at: Baidu.