-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2712.cpp
More file actions
51 lines (35 loc) · 960 Bytes
/
2712.cpp
File metadata and controls
51 lines (35 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class Solution {
public:
long long minimumCost(string s) {
ll n = s.length();
if(n == 1) return 0LL;
ll cost = 0;
ll oc = 0;
ll zc = 0;
ll m = n/2;
//check for one
if(s[m-1] != '1') oc += m;
for(int i = m-2; i >= 0; --i){
if(s[i] != s[i+1]) oc += (ll)i+1LL;
}
if(s[m] != '1') oc += n-m;
for(int i = m+1; i < n; ++i){
if(s[i] != s[i-1]) oc += n-(ll)i;
}
//check for zero
if(s[m-1] != '0') zc += m;
for(int i = m-2; i >= 0; --i){
if(s[i] != s[i+1]) zc += (ll)i+1LL;
}
if(s[m] != '0') zc += n-m;
for(int i = m+1; i < n; ++i){
if(s[i] != s[i-1]) zc += n-(ll)i;
}
//
cost = min(oc, zc);
return cost;
}
};