You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
6
+
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
7
+
vector must contain n/2 elements and now for final answer I simply added the difference of each element in o and c at same index to ans to calculate cost
8
+
which was initially zero and then printed the ans.
Problem: Find the mininmum number of 1 ohm resistance needed to make equivalent resistance a/b
5
+
6
+
The Calkin-Wilf Tree
7
+
This problem can be modeled using the Calkin-Wilf Tree, a binary tree that generates every positive rational number exactly once.
8
+
Starting from 1/1, the two possible operations are:
9
+
1. Series: a/b -> (a+b)/b (Moves to the Right Child)
10
+
2. Parallel: a/b -> a/(a+b) (Moves to the Left Child)
11
+
12
+
Approach: If a resistance a/b can be obained with k resistors then it is clear that we can get resistance (a+b)/b(if 1 ohm in series) or
13
+
a/(a+b)(if 1 ohm in parellel) with k+1 resistance. So we just go in reverse direction if a is greater than b then the last step must have been adding resistors
14
+
in series so we add a/b to our answer and make a equal to a%b.. similarly if b is greater than a then The last step must have been adding resistors in parallel so
15
+
we add b/a in answer and make b=b%a. This process continues untile either a or b becomes zero as either of a and b can become zero only when one is
16
+
completely divisible by other and in this case we will get minimum number of resistor as the quotient.
17
+
18
+
Time Complexity: O(log(min(a, b))) as it is equivalent to the Euclidean GCD algorithm
0 commit comments