Description
Solutions
Submission
Minimum Flips
🔥 FULLTIME

Start with an initial string of zeros. Choose any digit to flip. When a digit is flipped, its value and those to the right switch state between 0 and 1. Given a target string of binary digits, determine the minimum number of flips required to achieve the target.

Function Description

Complete the function minimumFlips in the editor.

minimumFlips has the following parameter(s):

  • String target: a string of 0s and 1s to match
  • Returns

  • int: the minimum number of flips needed to obtain the target string
  • Example 1:

    Input:  target = "01011"
    Output: 2
    Explanation:
    Start with a string of 5 zeros, the same length string as the target. Initial String -> 00000 Flip the 3rd digit -> 00111 Flip the 2nd dight -> 01000 Flip the 4th dight -> 01011 3 flips are required to reach the target. The return value is 3.

    Example 2:

    Input:  target = "0011"
    Output: 1
    Explanation:
    The number of digits is length(target) = 4. Flip the 3rd digit to obtain the desired state: 0000 -> 0011 after 1 flip.

    Example 3:

    Input:  target = "1010"
    Output: 4
    Explanation:
    Flip the 4th, 3rd, 2nd, and 1st digit to produce 0000 -> 0001 -> 0010 -> 0101 -> 1010 after 4 operations.
    Constraints:
    • 1 <= length of target <= 105
    • 0 <= target[i] <= 1
    • The target string consits of digits 0 and 1
    Thumbnail 0
    Thumbnail 1
    Thumbnail 2
    Thumbnail 3
    Testcase

    Result
    Case 1

    input:

    output: