-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
68 lines (63 loc) · 2.01 KB
/
main.cpp
File metadata and controls
68 lines (63 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Source: https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end
// Title: Maximum Number of Operations to Move Ones to the End
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a **binary string** `s`.
//
// You can perform the following operation on the string **any** number of times:
//
// - Choose **any** index `i` from the string where `i + 1 < s.length` such that `s[i] == '1'` and `s[i + 1] == '0'`.
// - Move the character `s[i]` to the **right** until it reaches the end of the string or another `'1'`. For example, for `s = "010010"`, if we choose `i = 1`, the resulting string will be `s = "0**001**10"`.
//
// Return the **maximum** number of operations that you can perform.
//
// **Example 1:**
//
// ```
// Input: s = "1001101"
// Output: 4
// Explanation:
// We can perform the following operations:
// - Choose index `i = 0`. The resulting string is `s = "**001**1101"`.
// - Choose index `i = 4`. The resulting string is `s = "0011**01**1"`.
// - Choose index `i = 3`. The resulting string is `s = "001**01**11"`.
// - Choose index `i = 2`. The resulting string is `s = "00**01**111"`.
// ```
//
// **Example 2:**
//
// ```
// Input: s = "00111"
// Output: 0
// ```
//
// **Constraints:**
//
// - `1 <= s.length <= 10^5`
// - `s[i]` is either `'0'` or `'1'`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <string>
using namespace std;
// Loop from left to right.
//
// Count 1s during scanning.
// For each 10 pair, add the count to the answer.
class Solution {
public:
int maxOperations(string s) {
auto ans = 0;
auto count = 0;
auto prev = ' ';
for (auto curr : s) {
if (curr == '1') {
++count;
} else if (curr == '0' && prev == '1') {
ans += count;
}
prev = curr;
}
return ans;
}
};