diff --git a/Problems/Data-structures/Day-04/sol/Satwik/Solution1.cpp b/Problems/Data-structures/Day-04/sol/Satwik/Solution1.cpp new file mode 100644 index 00000000..9772f40e --- /dev/null +++ b/Problems/Data-structures/Day-04/sol/Satwik/Solution1.cpp @@ -0,0 +1,70 @@ +/* +Problem: find the minimum cost after replace each _ by brackets which follow RBS + +Approach: +Use a stack which hold index for '(' or '_' if stack is empty. +'_' = '(' - if empty stack. +'_' = ')' - if not empty. + +Complexity: +Time: O(n) +Space: O(n): for the stack. +*/ + +#include +using namespace std; +#define ll long long +#define vll vector +#define out(x) cout << (x) << endl +#define outr(x) \ + { \ + cout << x << endl; \ + return; \ + } + +void solve() +{ + ll n; + cin >> n; + string s; + cin >> s; + + ll res = 0; + stack st; + for (ll i = 0; i < n; i++) + { + if (s[i] == '(') + st.push(i); + else if (s[i] == '_') + { + if (st.empty()) + { + st.push(i); + } + else + { + res += (i - st.top()); + st.pop(); + } + } + else + { + res += (i - st.top()); + st.pop(); + } + } + + out(res); +} + +int main() +{ + ll t; + cin >> t; + while (t--) + { + solve(); + } +} + +//submission link: https://codeforces.com/problemset/submission/1997/355793929 \ No newline at end of file