1529. Minimum Suffix Flips Medium

@problem@discussion
#String#Greedy



1/**
2 * [1529] Minimum Suffix Flips
3 *
4 * 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.
5 * 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'.
6 * Return the minimum number of operations needed to make s equal to target.
7 *  
8 * Example 1:
9 * 
10 * Input: target = "10111"
11 * Output: 3
12 * Explanation: Initially, s = "00000".
13 * Choose index i = 2: "00<u>000</u>" -> "00<u>111</u>"
14 * Choose index i = 0: "<u>00111</u>" -> "<u>11000</u>"
15 * Choose index i = 1: "1<u>1000</u>" -> "1<u>0111</u>"
16 * We need at least 3 flip operations to form target.
17 * 
18 * Example 2:
19 * 
20 * Input: target = "101"
21 * Output: 3
22 * Explanation: Initially, s = "000".
23 * Choose index i = 0: "<u>000</u>" -> "<u>111</u>"
24 * Choose index i = 1: "1<u>11</u>" -> "1<u>00</u>"
25 * Choose index i = 2: "10<u>0</u>" -> "10<u>1</u>"
26 * We need at least 3 flip operations to form target.
27 * 
28 * Example 3:
29 * 
30 * Input: target = "00000"
31 * Output: 0
32 * Explanation: We do not need any operations since the initial s already equals target.
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	n == target.length
38 * 	1 <= n <= 10^5
39 * 	target[i] is either '0' or '1'.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/minimum-suffix-flips/
45// discuss: https://leetcode.com/problems/minimum-suffix-flips/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn min_flips(target: String) -> i32 {
51        0
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_1529() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.