All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1501_1600.s1529_bulb_switcher_iv.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1501_1600.s1529_bulb_switcher_iv;

// #Medium #String #Greedy #2022_04_09_Time_6_ms_(89.67%)_Space_48.3_MB_(7.02%)

/**
 * 1529 - Minimum Suffix Flips\.
 *
 * Medium
 *
 * 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`.
 *
 * **Example 1:**
 *
 * **Input:** target = "10111"
 *
 * **Output:** 3
 *
 * **Explanation:** Initially, s = "00000". 
 *
 * Choose index i = 2: "00000" -> "00111" 
 *
 * Choose index i = 0: "00111" -> "11000" 
 *
 * Choose index i = 1: "11000" -> "10111" 
 *
 * We need at least 3 flip operations to form target.
 *
 * **Example 2:**
 *
 * **Input:** target = "101"
 *
 * **Output:** 3
 *
 * **Explanation:** Initially, s = "000". 
 *
 * Choose index i = 0: "000" -> "111" 
 *
 * Choose index i = 1: "111" -> "100" 
 *
 * Choose index i = 2: "100" -> "101" 
 *
 * We need at least 3 flip operations to form target.
 *
 * **Example 3:**
 *
 * **Input:** target = "00000"
 *
 * **Output:** 0
 *
 * **Explanation:** We do not need any operations since the initial s already equals target.
 *
 * **Constraints:**
 *
 * *   `n == target.length`
 * *   1 <= n <= 105
 * *   `target[i]` is either `'0'` or `'1'`.
**/
public class Solution {
    public int minFlips(String target) {
        int flipCount = target.charAt(0) - 48;
        char prev = target.charAt(0);
        for (char ch : target.toCharArray()) {
            if (ch != prev) {
                flipCount++;
                prev = ch;
            }
        }
        return flipCount;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy