Skip to content

Commit 4d15dbf

Browse files
chore: add LeetCode daily solution
1 parent b3f7fd7 commit 4d15dbf

5 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Number of Steps to Reduce a Number in Binary Representation to One (Medium)
2+
3+
**Problem ID:** 1404
4+
**Date:** 2026-02-26
5+
**Link:** https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/
6+
7+
## Approach
8+
9+
To solve the problem of determining the number of steps to reduce a binary representation of a number to one, we can follow a systematic approach based on the properties of binary numbers.
10+
11+
### Problem Breakdown:
12+
1. **Understanding the Operations**:
13+
- If the number is **even** (last digit is '0'), we divide it by 2.
14+
- If the number is **odd** (last digit is '1'), we add 1 to it.
15+
16+
2. **Binary Representation**:
17+
- The binary string represents a number in base-2. We can determine if the number is even or odd by checking the last character of the string.
18+
19+
### Approach:
20+
1. **Initialization**:
21+
- Start with a counter initialized to zero to track the number of steps.
22+
23+
2. **Iterate through the Binary String**:
24+
- While the binary string is not equal to "1":
25+
- Check if the last character of the string is '0' or '1':
26+
- If it is '0', it means the number is even. We can simply remove the last character (simulate division by 2) and increment the step counter.
27+
- If it is '1', it means the number is odd. We need to add 1, which means we will effectively convert the last '1' to '0' and potentially change other bits. This can be simulated by:
28+
- Incrementing the step counter.
29+
- Changing the last '1' to '0' and then appending a '0' at the end (to represent the addition of 1).
30+
- Repeat this until we reach "1".
31+
32+
3. **Count Steps**:
33+
- Each operation (either division or addition) increments the step counter by one.
34+
35+
### Data Structures:
36+
- A simple string manipulation is sufficient for this problem. We can use a string to represent the binary number and utilize string slicing to simulate the operations.
37+
38+
### Complexity:
39+
- **Time Complexity**: O(n), where n is the length of the binary string. In the worst case, we may traverse the entire string.
40+
- **Space Complexity**: O(1), as we are using a constant amount of extra space for the counter and do not require additional data structures that grow with input size.
41+
42+
### Conclusion:
43+
By systematically applying the rules for even and odd numbers based on the binary representation, we can efficiently count the number of steps needed to reduce the number to one. The approach leverages string manipulation to simulate the operations without needing to convert the binary string to its decimal equivalent explicitly.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int numSteps(String s) {
3+
int steps = 0;
4+
int n = s.length();
5+
6+
for (int i = n - 1; i > 0; i--) {
7+
if (s.charAt(i) == '1') {
8+
steps += 2; // Add 1 and then divide by 2
9+
} else {
10+
steps += 1; // Just divide by 2
11+
}
12+
}
13+
14+
return steps;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var numSteps = function(s) {
2+
let steps = 0;
3+
let carry = 0;
4+
5+
for (let i = s.length - 1; i > 0; i--) {
6+
if (s[i] === '0') {
7+
steps++; // Even case: divide by 2
8+
} else {
9+
steps += 2; // Odd case: add 1 and then divide by 2
10+
}
11+
}
12+
13+
return steps + carry;
14+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def numSteps(self, s: str) -> int:
3+
steps = 0
4+
while s != "1":
5+
if s[-1] == '0':
6+
s = s[:-1] # Remove the last character (divide by 2)
7+
else:
8+
s = bin(int(s, 2) + 1)[2:] # Add 1 and convert back to binary
9+
steps += 1
10+
return steps

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
376376
- 2026-02-23 — [Check If a String Contains All Binary Codes of Size K](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/) (Medium) → `Medium/2026-02-23-1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K`
377377
- 2026-02-24 — [Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/) (Easy) → `Easy/2026-02-24-1022-Sum-of-Root-To-Leaf-Binary-Numbers`
378378
- 2026-02-25 — [Sort Integers by The Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/) (Easy) → `Easy/2026-02-25-1356-Sort-Integers-by-The-Number-of-1-Bits`
379+
- 2026-02-26 — [Number of Steps to Reduce a Number in Binary Representation to One](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/) (Medium) → `Medium/2026-02-26-1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One`

0 commit comments

Comments
 (0)