Input: s ="0011"Output: 2Explanation: Apply the second operation with i =2 to obtain s ="0000"for a cost of 2. It can be shown that 2is the minimum cost to make all characters equal.
Input: s ="010101"Output: 9Explanation: Apply the first operation with i =2 to obtain s ="101101"for a cost of 3.Apply the first operation with i =1 to obtain s ="011101"for a cost of 2.Apply the first operation with i =0 to obtain s ="111101"for a cost of 1.Apply the second operation with i =4 to obtain s ="111110"for a cost of 2.Apply the second operation with i =5 to obtain s ="111111"for a cost of 1.The total cost to make all characters equal is9. It can be shown that 9is the minimum cost to make all characters equal.
To make all characters equal, we can use either prefix or suffix invert operations. The optimal strategy is to find the minimum cost to make all characters ‘0’ or all ‘1’ by considering the cost of flipping at each index from left to right and right to left.
classSolution {
public:longlong minimumCost(string s) {
int n = s.size();
longlong ans = LLONG_MAX;
for (char tgt : {'0', '1'}) {
longlong cost =0;
for (int i =0; i < n; ++i) {
if (s[i] != tgt) cost += i +1;
}
ans = min(ans, cost);
cost =0;
for (int i = n-1; i >=0; --i) {
if (s[i] != tgt) cost += n - i;
}
ans = min(ans, cost);
}
return ans;
}
};
classSolution {
publiclongminimumCost(String s) {
int n = s.length();
long ans = Long.MAX_VALUE;
for (char tgt : newchar[]{'0', '1'}) {
long cost = 0;
for (int i = 0; i < n; ++i) {
if (s.charAt(i) != tgt) cost += i + 1;
}
ans = Math.min(ans, cost);
cost = 0;
for (int i = n-1; i >= 0; --i) {
if (s.charAt(i) != tgt) cost += n - i;
}
ans = Math.min(ans, cost);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funminimumCost(s: String): Long {
val n = s.length
var ans = Long.MAX_VALUE
for (tgt in listOf('0', '1')) {
var cost = 0Lfor (i in0 until n) {
if (s[i] != tgt) cost += (i+1).toLong()
}
ans = minOf(ans, cost)
cost = 0Lfor (i in n-1 downTo 0) {
if (s[i] != tgt) cost += (n-i).toLong()
}
ans = minOf(ans, cost)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defminimumCost(self, s: str) -> int:
n = len(s)
ans = float('inf')
for tgt in'01':
cost = sum(i+1for i, c in enumerate(s) if c != tgt)
ans = min(ans, cost)
cost = sum(n-i for i, c in enumerate(s) if c != tgt)
ans = min(ans, cost)
return ans
impl Solution {
pubfnminimum_cost(s: String) -> i64 {
let n = s.len();
let s = s.as_bytes();
letmut ans =i64::MAX;
for&tgt in&[b'0', b'1'] {
letmut cost =0;
for i in0..n {
if s[i] != tgt { cost += (i+1) asi64; }
}
ans = ans.min(cost);
cost =0;
for i in (0..n).rev() {
if s[i] != tgt { cost += (n-i) asi64; }
}
ans = ans.min(cost);
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
minimumCost(s: string):number {
constn=s.length;
letans= Number.MAX_SAFE_INTEGER;
for (consttgtof ['0', '1']) {
letcost=0;
for (leti=0; i<n; ++i) {
if (s[i] !==tgt) cost+=i+1;
}
ans= Math.min(ans, cost);
cost=0;
for (leti=n-1; i>=0; --i) {
if (s[i] !==tgt) cost+=n-i;
}
ans= Math.min(ans, cost);
}
returnans;
}
}