Skip to content

Commit 4c48e78

Browse files
chore: add LeetCode daily solution
1 parent 64c58a3 commit 4c48e78

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Minimum One Bit Operations to Make Integers Zero (Hard)
2+
3+
**Problem ID:** 1611
4+
**Date:** 2025-11-08
5+
**Link:** https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/
6+
7+
## Approach
8+
9+
To solve the problem of transforming an integer \( n \) to zero using the minimum number of specified bit operations, we can leverage a recursive approach with memoization or a bit manipulation strategy.
10+
11+
### Main Idea:
12+
The key insight is to recognize that the operations allowed are closely related to the structure of the binary representation of \( n \). Specifically, the operations manipulate the rightmost bits and depend on the state of adjacent bits. The goal is to minimize the number of operations needed to turn all bits of \( n \) to zero.
13+
14+
### Approach:
15+
1. **Recursive Function**: Define a recursive function `minOperations(n)` that calculates the minimum operations required to reduce \( n \) to zero. The base case is when \( n \) is zero, in which case no operations are needed.
16+
17+
2. **Bit Manipulation**: For each recursive call:
18+
- If the rightmost bit (0th bit) is 1, we can perform the first operation (change the 0th bit to 0) and then call `minOperations(n >> 1)` (which effectively divides \( n \) by 2).
19+
- If the rightmost bit is 0, we need to check the next bits. If the next bit is 1, we can perform the second operation, which allows us to change the current bit while ensuring the previous bits are set correctly. This might involve a more complex manipulation of bits, and we may need to explore the structure of \( n \) further.
20+
21+
3. **Memoization**: To avoid recalculating results for the same values of \( n \), we can store previously computed results in a dictionary or an array.
22+
23+
### Complexity:
24+
- **Time Complexity**: The time complexity is \( O(\log n) \) due to the recursive nature of the bit manipulation, as the number of bits in \( n \) is logarithmic relative to its value.
25+
- **Space Complexity**: The space complexity is \( O(\log n) \) as well, primarily due to the recursion stack and the memoization storage.
26+
27+
### Conclusion:
28+
By carefully considering the binary representation of \( n \) and applying recursive strategies with memoization, we can efficiently determine the minimum number of operations required to transform \( n \) into zero. This approach balances clarity and efficiency, leveraging the properties of binary numbers and operations.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int minimumOneBitOperations(int n) {
3+
int operations = 0;
4+
while (n > 0) {
5+
operations += n;
6+
n = n >> 1;
7+
}
8+
return operations;
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var minimumOneBitOperations = function(n) {
2+
let operations = 0;
3+
while (n > 0) {
4+
operations ^= n; // XOR with n
5+
n >>= 1; // Right shift n
6+
}
7+
return operations;
8+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def minimumOneBitOperations(self, n: int) -> int:
3+
if n == 0:
4+
return 0
5+
return n + self.minimumOneBitOperations(n >> 1)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
272272
- 2025-11-05 — [Find X-Sum of All K-Long Subarrays II](https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-ii/) (Hard) → `Hard/2025-11-05-3321-Find-X-Sum-of-All-K-Long-Subarrays-II`
273273
- 2025-11-06 — [Power Grid Maintenance](https://leetcode.com/problems/power-grid-maintenance/) (Medium) → `Medium/2025-11-06-3607-Power-Grid-Maintenance`
274274
- 2025-11-07 — [Maximize the Minimum Powered City](https://leetcode.com/problems/maximize-the-minimum-powered-city/) (Hard) → `Hard/2025-11-07-2528-Maximize-the-Minimum-Powered-City`
275+
- 2025-11-08 — [Minimum One Bit Operations to Make Integers Zero](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/) (Hard) → `Hard/2025-11-08-1611-Minimum-One-Bit-Operations-to-Make-Integers-Zero`

0 commit comments

Comments
 (0)