-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay40.cpp
More file actions
53 lines (38 loc) · 1.15 KB
/
Day40.cpp
File metadata and controls
53 lines (38 loc) · 1.15 KB
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
52
53
/*
This problem was asked by LinkedIn.
You are given a string consisting of the letters x and y, such as xyxxxyxyy. In addition, you have an operation called flip, which changes a single x to y or vice versa.
Determine how many times you would need to apply this operation to ensure that all x's come before all y's. In the preceding example, it suffices to flip the second and sixth characters, so you should return 2.
*/
#include <bits/stdc++.h>
using namespace std;
int minFlipsMonoXY(string s)
{
int totalX = 0;
for (char ch : s)
if (ch == 'x')
totalX++;
int flipY = 0, flipX = totalX;
int minFlips = INT_MAX;
for (char ch : s)
{
minFlips = min(minFlips, flipX + flipY);
if (ch == 'y')
{
flipY++;
}
else
{
flipX--;
}
}
minFlips = min(minFlips, flipX + flipY);
return minFlips;
}
int main()
{
string input = "xyxxxyxyy"; // 🔧 Change this to test other inputs
int result = minFlipsMonoXY(input);
cout << "Input string: " << input << endl;
cout << "Minimum flips required: " << result << endl;
return 0;
}