java.lang.Object
g2701_2800.s2712_minimum_cost_to_make_all_characters_equal.Solution

public class Solution extends Object
2712 - Minimum Cost to Make All Characters Equal.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code> on which you can apply two types of operations:</p> <ul> <li>Choose an index <code>i</code> and invert all characters from index <code>0</code> to index <code>i</code> (both inclusive), with a cost of <code>i + 1</code></li> <li>Choose an index <code>i</code> and invert all characters from index <code>i</code> to index <code>n - 1</code> (both inclusive), with a cost of <code>n - i</code></li> </ul> <p>Return <em>the <strong>minimum cost</strong> to make all characters of the string <strong>equal</strong></em>.</p> <p><strong>Invert</strong> a character means if its value is &lsquo;0&rsquo; it becomes &lsquo;1&rsquo; and vice-versa.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;0011&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> Apply the second operation with <code>i = 2</code> to obtain <code>s = &quot;0000&quot; for a cost of 2</code>. It can be shown that 2 is the minimum cost to make all characters equal.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;010101&rdquo;</p> <p><strong>Output:</strong> 9</p> <p><strong>Explanation:</strong></p> <p>Apply the first operation with i = 2 to obtain s = &ldquo;101101&rdquo; for a cost of 3.</p> <p>Apply the first operation with i = 1 to obtain s = &ldquo;011101&rdquo; for a cost of 2.</p> <p>Apply the first operation with i = 0 to obtain s = &ldquo;111101&rdquo; for a cost of 1.</p> <p>Apply the second operation with i = 4 to obtain s = &ldquo;111110&rdquo; for a cost of 2.</p> <p>Apply the second operation with i = 5 to obtain s = &ldquo;111111&rdquo; for a cost of 1.</p> <p>The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make all characters equal.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length == n <= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimumCost

      public long minimumCost(String s)