We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 41224ec commit 0fd04f1Copy full SHA for 0fd04f1
Solutions/MinimumFlips.cpp
@@ -0,0 +1,26 @@
1
+class Solution {
2
+public:
3
+ int minFlips(string s) {
4
+ int originalLength = s.size();
5
+ string formed = s + s;
6
+ int minScore = 1e9;
7
+ int expected = 0;
8
+ int score1 = 0 , score2 = 0 ;
9
+ for ( int i= 0 ; i < formed.size() ; i++ ){
10
+
11
+ if ( formed[i] - '0' != expected ) score1++;
12
+ if ( formed[i] - '0' == expected ) score2++;
13
+ if ( i >= originalLength ){
14
+ int expback = expected ^ ( originalLength & 1);
15
+ if ( formed[i] - '0' != expback ) score1--;
16
+ if ( formed[i] - '0' == expback ) score2--;
17
+ }
18
+ if ( i + 1 >= originalLength ){
19
+ minScore = min({ minScore , score1, score2 } );
20
21
+ expected ^= 1;
22
23
24
+ return minScore;
25
26
+};
0 commit comments