[LeetCode] Minimum Suffix Flips

1529. Minimum Suffix Flips

You are given a 0-indexed binary string target of length n. You have another binary string s of length n that is initially set to all zeros. You want to make s equal to target.

In one operation, you can pick an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Flip means changing ‘0’ to ‘1’ and ‘1’ to ‘0’.

Return the minimum number of operations needed to make s equal to target

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int minFlips(string target) {
int now = 0, res = 0;
for(auto& ch : target) {
int v = ch & 0b1111;
if(now == v) continue;
res++;
now = !now;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/19/PS/LeetCode/minimum-suffix-flips/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.