Skip to content

Commit 7733218

Browse files
committed
Merge branch 'main' of https://github.com/aricthecoder/CP-Chronicles into day5sol2
2 parents 60749c8 + 7c850b1 commit 7733218

5 files changed

Lines changed: 128 additions & 2 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Problem Link- https://codeforces.com/problemset/problem/1997/C
2+
//Submission Link- https://codeforces.com/problemset/submission/1997/355848236
3+
//checked greedy pairing of current index with last one in stack
4+
//time complexity- O(n)
5+
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
void solve() {
10+
int n;
11+
string s;
12+
cin>>n>>s;
13+
stack <pair<char,int>> s1;
14+
int cost=0;
15+
for(int i=0;i<n;i++){
16+
if(s1.empty()){
17+
s1.push({s[i],i});
18+
}else{
19+
if(s[i]==')'&&s1.top().first=='_'){
20+
cost+=(i-s1.top().second);
21+
s1.pop();
22+
}else if(s[i]=='('&&s1.top().first=='_'){
23+
s1.push({s[i],i});
24+
}else if(s[i]=='_'&&s1.top().first=='('){
25+
cost+=(i-s1.top().second);
26+
s1.pop();
27+
}
28+
}
29+
}
30+
31+
cout<<cost<<endl;
32+
}
33+
34+
signed main() {
35+
36+
int t;
37+
cin >> t;
38+
39+
while (t--) {
40+
solve();
41+
}
42+
43+
return 0;
44+
}

Problems/Data-structures/Day-04/sol/Naman Pal/day004sol01.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Submission Link: https://codeforces.com/contest/1997/submission/355835686
22

3-
43
/*
54
So first I made the RBS with minimmum cost by putting closing bracket as soon as we get a opening bracket before it, i.e., closing bracket as early as
65
possible(greedy appraoch). After getting corect bracket sequence I put the indexes of '(' in vector o and indexes of ')' in vector c in order they occur. Both
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// link : https://codeforces.com/contest/343/submission/355845658
2+
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
int main() {
8+
long long a,b;
9+
cin>>a>>b;
10+
long long ans=0;
11+
while(a>0 && b>0) {
12+
if(a>b) {
13+
ans+=a/b;
14+
a=a%b;
15+
}
16+
else {
17+
ans+=b/a;
18+
b=b%a;
19+
}
20+
}
21+
cout<<ans<<endl;
22+
}
23+
24+
25+
26+
/*
27+
Explanation:
28+
29+
We need to construct a resistance equal to a / b using unit resistors.
30+
Series and parallel connections correspond to increasing or reducing the
31+
fraction exactly like steps in the Euclidean Algorithm.
32+
33+
If a > b:
34+
- We can add 1 in series floor(a / b) times
35+
- a becomes a % b
36+
37+
If b > a:
38+
- We can add 1 in parallel floor(b / a) times
39+
- b becomes b % a
40+
41+
Each division step adds the quotient to the number of resistors used.
42+
Thus, the answer is the sum of all quotients during the Euclidean process.
43+
44+
The fraction is irreducible, so the algorithm always terminates.
45+
46+
Time Complexity:
47+
O(log(min(a, b)))
48+
49+
Space Complexity:
50+
O(1)
51+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
We needed to find out the max no. of resistors needed, to get to a target resistance, with only 1 unit resistors.
3+
4+
I wanted to convert the resistance to 1/1, so I took the larger no. and subtracted it by the maximum multiple of lower number, which is still lower than the larger one, adding 1 each time.
5+
6+
Time: O(log(max(a, b)))
7+
Space: O(1)
8+
9+
Submission Link: https://codeforces.com/contest/343/submission/355851930
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
using namespace std;
14+
15+
using ll = long long;
16+
17+
int main() {
18+
ll a, b;
19+
cin >> a >> b;
20+
21+
ll ans = 1;
22+
while (a != 1 || b != 1) {
23+
if (a > b) swap(a, b);
24+
// b is always greater
25+
26+
ll count = (b-1) / a;
27+
ans += count;
28+
b -= count * a;
29+
}
30+
31+
cout << ans;
32+
}

Problems/Mathematics/Day-03/sol/Naman Pal/day003sol02.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Submission Link: https://codeforces.com/contest/343/submission/355828800
1+
// Submission Link: https://codeforces.com/contest/343/submission/355828800
22

33
/*
44
Problem: Find the mininmum number of 1 ohm resistance needed to make equivalent resistance a/b

0 commit comments

Comments
 (0)