Leetcode 397: Integer Replacement
Given a positive integer
n
, you can perform one of the following operations: If n
is even, divide n
by 2. If n
is odd, you can either add 1 to n
or subtract 1 from n
. Your task is to return the minimum number of operations required to reduce n
to 1.Problem
Approach
Steps
Complexity
Input: The input consists of a single integer `n` (1 <= n <= 2^31 - 1).
Example: Input: n = 6
Constraints:
• 1 <= n <= 2^31 - 1
Output: The output is a single integer representing the minimum number of operations required to reduce `n` to 1.
Example: Output: 3
Constraints:
• The solution must return the minimum number of operations required.
Goal: The goal is to minimize the number of operations required to reduce `n` to 1 by strategically choosing the best operation at each step.
Steps:
• If `n` is even, divide by 2.
• If `n` is odd, choose either `n + 1` or `n - 1` based on which operation leads to a smaller number of operations.
Goal: The solution must efficiently handle large values of `n` up to 2^31 - 1.
Steps:
• The solution should handle input sizes up to 2^31 - 1.
Assumptions:
• The input integer `n` is always a valid positive integer.
• Input: Input: n = 6
• Explanation: We perform operations on `6`: 6 -> 3 -> 2 -> 1, taking 3 steps.
• Input: Input: n = 15
• Explanation: We perform operations on `15`: 15 -> 16 -> 8 -> 4 -> 2 -> 1, taking 5 steps.
Approach: The approach uses a greedy algorithm to perform the optimal operation at each step, choosing either `n + 1` or `n - 1` based on the parity of the number.
Observations:
• For even numbers, dividing by 2 is always optimal.
• For odd numbers, we need to decide whether adding or subtracting 1 leads to fewer steps.
• We need to use the parity of `n` to decide the most efficient operation for odd numbers.
Steps:
• Check if `n` is even or odd.
• If even, divide by 2.
• If odd, choose the operation that minimizes the remaining steps (add 1 or subtract 1).
• Repeat the process until `n` reaches 1.
Empty Inputs:
• The input is always a valid integer `n` between 1 and 2^31 - 1.
Large Inputs:
• Ensure the solution can handle large values of `n` (up to 2^31 - 1) efficiently.
Special Values:
• Handle small inputs like `n = 1` directly (no operations needed).
Constraints:
• Handle edge cases where `n` is very small or very large.
int integerReplacement(int n) {
if(n == INT_MAX) return 32;
int cnt = 0;
while(n > 1) {
if(n % 2 == 0) n /= 2;
else {
if((n + 1) %4 == 0 && (n - 1) != 2) n++;
else n--;
}
cnt++;
}
return cnt;
}
1 : Function Definition
int integerReplacement(int n) {
This line defines the function `integerReplacement`, which takes an integer `n` as input and returns an integer representing the minimum number of steps to reduce `n` to 1.
2 : Edge Case Check
if(n == INT_MAX) return 32;
If `n` is the maximum integer value (`INT_MAX`), the function returns 32 directly, as it would require 32 operations to reduce it to 1.
3 : Variable Initialization
int cnt = 0;
The variable `cnt` is initialized to count the number of operations performed to reduce `n` to 1.
4 : Loop Setup
while(n > 1) {
A `while` loop is used to continue the process until `n` becomes 1.
5 : Even Case Handling
if(n % 2 == 0) n /= 2;
If `n` is even (i.e., divisible by 2), it is halved as the next operation.
6 : Odd Case Handling
else {
If `n` is odd, we need to either increment or decrement it.
7 : Increment Case
if((n + 1) % 4 == 0 && (n - 1) != 2) n++;
If incrementing `n` leads to a number divisible by 4 and it is not equal to 2, the function increments `n`.
8 : Decrement Case
else n--;
If the condition for incrementing is not satisfied, the function decrements `n`.
9 : Operation Count
cnt++;
Increment the counter `cnt` to keep track of the number of operations performed.
10 : Return Result
return cnt;
After reducing `n` to 1, the function returns the total number of operations performed.
Best Case: O(log n)
Average Case: O(log n)
Worst Case: O(log n)
Description: The time complexity is logarithmic because each operation reduces the number by at least half.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is constant as we only need a few variables to track the current state.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus