|
1 | 1 | ## Explanation |
2 | 2 |
|
3 | | -### Strategy |
| 3 | +### Strategy (The "Why") |
4 | 4 |
|
5 | | -**Constraints & Edge Cases** |
| 5 | +**Restate the problem:** We need to find the minimum number of bit flips required to make an integer equal to its binary reverse. The binary reverse of a number is obtained by reversing its binary representation. |
6 | 6 |
|
7 | | - * **Binary Representation:** We are working with the bits of an integer $n$. The length of the binary string depends on the magnitude of $n$ (up to $\approx 30$ bits for standard integers). |
8 | | - * **Time Complexity:** Since the number of bits is small ($\log_2 n$), the solution will be very fast, effectively $O(\log n)$. |
9 | | - * **Edge Case:** If $n=0$ or $n$ is a single bit (e.g., 1), the reverse is identical to the original, so the answer is 0. |
| 7 | +**1.1 Constraints & Complexity:** |
10 | 8 |
|
11 | | -**High-level approach** |
12 | | -The problem asks for the minimum flips to make a number equal to its **original** binary reverse. |
13 | | -Imagine the binary string as a row of lights. We compare the light at the very start (left) with the light at the very end (right). |
| 9 | +- **Input Size:** The integer n can be up to 2^31 - 1, which means up to approximately 31 bits in binary representation. |
| 10 | +- **Time Complexity:** O(log n) - we need to process each bit in the binary representation once, and the number of bits is logarithmic in n. |
| 11 | +- **Space Complexity:** O(log n) - we store the binary string representation, which has length proportional to log n. |
| 12 | +- **Edge Case:** If n = 0 or n has only one bit (e.g., 1), the reverse is identical to the original, so the answer is 0. |
14 | 13 |
|
15 | | - * If the left bit is `1` and the right bit is `0`, we have a mismatch. |
16 | | - * To make the number equal to its reverse, the left position *must* become what the right position was, and the right position *must* become what the left position was. This requires flipping **both** bits. |
17 | | - * Therefore, every mismatching symmetric pair costs exactly **2 flips**. |
| 14 | +**1.2 High-level approach:** |
18 | 15 |
|
19 | | -**Brute force vs. optimized strategy** |
| 16 | +The goal is to compare symmetric bit positions in the binary representation and count how many pairs need to be flipped to make the number equal to its reverse. |
20 | 17 |
|
21 | | - * **Brute Force:** Calculate the reverse of $n$ separately, then iterate through every bit of $n$ and the reverse to count differences. |
22 | | - * **Optimized (Two Pointers):** We extract the binary string once. We place pointers at the start and end. We move them inward, counting mismatches. This is efficient and requires only one pass over the bits. |
| 18 | + |
23 | 19 |
|
24 | | -**Decomposition** |
| 20 | +**1.3 Brute force vs. optimized strategy:** |
25 | 21 |
|
26 | | -1. **Bit Extraction:** Convert the integer $n$ into its binary string format (removing the '0b' prefix). |
27 | | -2. **Two-Pointer Scan:** Initialize `left` at index 0 and `right` at the last index. |
28 | | -3. **Check Pairs:** If `s[left] != s[right]`, we found a mismatch. Add 2 to our result (1 flip for each side). |
29 | | -4. **Converge:** Move `left` forward and `right` backward until they meet. |
| 22 | +- **Brute Force:** Calculate the reverse of n separately, then iterate through every bit of n and the reverse to count differences. This requires creating the full reverse and comparing bit by bit. |
| 23 | +- **Optimized Strategy:** Use two pointers starting from both ends of the binary string, moving inward while comparing symmetric bits. This is O(log n) time and only requires one pass. |
| 24 | +- **Optimization:** By using two pointers, we avoid creating a separate reversed binary string and can process the comparison in a single pass, making the solution more efficient. |
30 | 25 |
|
31 | | -### Steps |
| 26 | +**1.4 Decomposition:** |
32 | 27 |
|
33 | | -1. **Convert to Binary** |
34 | | - Turn the integer $n$ into a string of bits. For example, if $n=10$, we get the string `"1010"`. |
| 28 | +1. Convert the integer to its binary string representation (removing the '0b' prefix). |
| 29 | +2. Initialize two pointers at the start and end of the binary string. |
| 30 | +3. Compare bits at symmetric positions (left and right pointers). |
| 31 | +4. If bits differ, we need to flip both bits (add 2 to the result). |
| 32 | +5. Move pointers inward until they meet. |
| 33 | +6. Return the total number of flips needed. |
35 | 34 |
|
36 | | -2. **Initialize Pointers** |
37 | | - Set a variable `res` to 0. Set `l` to the start of the string and `r` to the end. |
| 35 | +### Steps (The "How") |
38 | 36 |
|
39 | | -3. **Iterate and Compare** |
40 | | - While `l` is less than `r`: |
| 37 | +**2.1 Initialization & Example Setup:** |
41 | 38 |
|
42 | | - * Check if the bit at `l` is different from the bit at `r`. |
43 | | - * **Why?** If the bits are different (e.g., `1` and `0`), then to swap their values effectively (so the number equals its reverse), both positions must be flipped. |
44 | | - * If they differ, add **2** to `res`. |
| 39 | +Let's use the example: `n = 10` |
45 | 40 |
|
46 | | -4. **Close the Window** |
47 | | - Increment `l` and decrement `r` to check the next pair of bits. Continue until the pointers meet in the middle. |
| 41 | +- Binary representation: `bin(10) = "0b1010"`, so `s = "1010"` |
| 42 | +- Left pointer `l = 0`, right pointer `r = 3` |
| 43 | +- Result `res = 0` |
| 44 | + |
| 45 | +**2.2 Start Checking:** |
| 46 | + |
| 47 | +We begin comparing bits from both ends of the binary string. |
| 48 | + |
| 49 | +**2.3 Trace Walkthrough:** |
| 50 | + |
| 51 | +| Step | l | r | s[l] | s[r] | Match? | Action | res | |
| 52 | +| ---- | --- | --- | ---- | ---- | ------ | --------------------- | --- | |
| 53 | +| 1 | 0 | 3 | '1' | '0' | No | Flip both bits, add 2 | 2 | |
| 54 | +| 2 | 1 | 2 | '0' | '1' | No | Flip both bits, add 2 | 4 | |
| 55 | + |
| 56 | +**2.4 Increment and Loop:** |
| 57 | + |
| 58 | +After each comparison, we increment `l` and decrement `r` to move toward the center. We continue until `l >= r`. |
| 59 | + |
| 60 | +**2.5 Return Result:** |
| 61 | + |
| 62 | +The result is 4, which means we need to flip 4 bits (2 pairs) to make the number equal to its binary reverse. |
0 commit comments