File tree Expand file tree Collapse file tree
Problems/Data-structures/Day-04/sol/BEESA_MANISH Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+
2+ // TC - O(n) and SC - O(n)
3+ // Submission Link - https://codeforces.com/contest/1997/submission/355786129
4+
5+ #include < iostream>
6+ #include < string>
7+ #include < vector>
8+ using namespace std ;
9+ void solve () {
10+ int n;
11+ cin >> n;
12+ string s;
13+ cin >> s;
14+ long long total_cost = 0 ;
15+ long long current_balance = 0 ;
16+ for (int i = 0 ; i < n; ++i) {
17+ if (s[i] == ' _' ) {
18+ if (current_balance == 0 ) {
19+ current_balance++;
20+ } else {
21+ current_balance--;
22+ }
23+ } else if (s[i] == ' (' ) {
24+ current_balance++;
25+ } else {
26+ current_balance--;
27+ }
28+ total_cost += current_balance;
29+ }
30+
31+ cout << total_cost << " \n " ;
32+ }
33+
34+ int main () {
35+ int t;
36+ cin >> t;
37+ while (t--) {
38+ solve ();
39+ }
40+ return 0 ;
41+ }
You can’t perform that action at this time.
0 commit comments