-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily113.cpp
59 lines (51 loc) · 1.42 KB
/
daily113.cpp
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
54
55
56
57
58
59
// Solution 1
class Solution {
public:
int minSwaps(string s) {
/*
store in a stack
every valid pair of [ ] is removed from the stack
every 1 mismatch, you replace 1
2- 1
3-2
4-2
5-3
6-3
7-4 etc
*/
auto remaining = std::stack<char>{};
for (auto i = 0; i < s.size(); ++i) {
if (s[i] == '[')
remaining.push(s[i]);
else {
if (remaining.empty())
remaining.push(s[i]);
else if (remaining.top() == '[')
remaining.pop();
else
remaining.push(s[i]);
}
// std::cout << "curr stack size: " << remaining.size() << std::endl;
}
auto divisor = remaining.size() < 4 ? 2.0 : 4.0;
// std::cout << divisor;
return static_cast<int>(std::ceil(remaining.size() / divisor));
}
};
// Solution 2
static const int __ = []() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); return 0; }();
class Solution {
public:
int minSwaps(string s) {
int count = 0;
for(auto& c : s){
if(c == '[') {
++count;
} else if(count) {
--count;
}
}
bool b = count & 1;
return (b + count >> 1);
}
};