Skip to content

Commit 96346b5

Browse files
authored
Merge pull request #362 from MANISH-BEESA/BEESA-MANISH
ADDED DAY4 Q1 SOLUTION
2 parents 1a1ba7d + f7f1593 commit 96346b5

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)