Skip to content

Commit 5fc3441

Browse files
chore: add LeetCode daily solution
1 parent 5697d4b commit 5fc3441

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Maximum Number of Operations to Move Ones to the End (Medium)
2+
3+
**Problem ID:** 3228
4+
**Date:** 2025-11-13
5+
**Link:** https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end/
6+
7+
## Approach
8+
9+
To solve the problem of maximizing the number of operations to move '1's to the end of a binary string, we can adopt a systematic approach:
10+
11+
### Problem Understanding
12+
The goal is to count how many times we can move a '1' to the right until it reaches either the end of the string or another '1'. Each operation can only be performed when a '1' is immediately followed by a '0'.
13+
14+
### Approach
15+
1. **Identify Pairs of '1's and '0's**: As we traverse the string, we need to keep track of the number of '1's that can potentially be moved. For each '1' encountered, we check how many '0's follow it until we hit another '1' or reach the end of the string.
16+
17+
2. **Count Movable '1's**: For each '1' found, count how many '0's are between it and the next '1'. The number of operations possible for that specific '1' is equal to the number of '0's encountered before the next '1'.
18+
19+
3. **Iterate Through the String**: We can maintain two counters:
20+
- `count_ones`: To keep track of the number of '1's encountered so far.
21+
- `count_operations`: To accumulate the total number of operations possible.
22+
23+
4. **Calculate Operations**: For each '1' found, add the current count of '0's (which can be moved) to `count_operations`. After processing a '0', if we encounter another '1', we reset the count of '0's since we can no longer move the previous '1' past this new '1'.
24+
25+
5. **Final Count**: The result will be stored in `count_operations`, which will represent the maximum number of operations performed.
26+
27+
### Data Structures
28+
- We primarily use simple integer counters (`count_ones`, `count_operations`, and `count_zeros`) as we traverse the string. No complex data structures are needed, making this approach efficient.
29+
30+
### Complexity
31+
- **Time Complexity**: O(n), where n is the length of the string. We make a single pass through the string.
32+
- **Space Complexity**: O(1), as we are only using a fixed number of integer variables for counting.
33+
34+
By following this approach, we efficiently determine the maximum number of operations to move '1's to the end of the string while adhering to the problem's constraints.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maximumOperations(String s) {
3+
int countOnes = 0;
4+
int operations = 0;
5+
6+
for (char c : s.toCharArray()) {
7+
if (c == '1') {
8+
countOnes++;
9+
} else if (countOnes > 0) {
10+
operations += countOnes;
11+
}
12+
}
13+
14+
return operations;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var maximumOperations = function(s) {
2+
let countOnes = 0;
3+
let operations = 0;
4+
5+
for (let char of s) {
6+
if (char === '1') {
7+
countOnes++;
8+
} else if (countOnes > 0) {
9+
operations++;
10+
}
11+
}
12+
13+
return operations;
14+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maximumOperations(self, s: str) -> int:
3+
count_ones = 0
4+
operations = 0
5+
6+
for char in s:
7+
if char == '1':
8+
count_ones += 1
9+
elif count_ones > 0:
10+
operations += count_ones
11+
12+
return operations

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
277277
- 2025-11-10 — [Minimum Operations to Convert All Elements to Zero](https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero/) (Medium) → `Medium/2025-11-10-3542-Minimum-Operations-to-Convert-All-Elements-to-Zero`
278278
- 2025-11-11 — [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) (Medium) → `Medium/2025-11-11-474-Ones-and-Zeroes`
279279
- 2025-11-12 — [Minimum Number of Operations to Make All Array Elements Equal to 1](https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/) (Medium) → `Medium/2025-11-12-2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1`
280+
- 2025-11-13 — [Maximum Number of Operations to Move Ones to the End](https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end/) (Medium) → `Medium/2025-11-13-3228-Maximum-Number-of-Operations-to-Move-Ones-to-the-End`

0 commit comments

Comments
 (0)