Minimum Bracket Reversals needed to make an expression balanced | Stack Data Structure
ฝัง
- เผยแพร่เมื่อ 12 ก.ย. 2024
- Question:- Given an expression with only ‘}’ and ‘{‘. The expression may not be balanced. Find minimum number of bracket reversals to make the expression balanced
Time Complexity:- O(n)
Space Complexity:- O(n)
Example 1:-
Input: exp = "}{"
Output: 2
Example 2:-
Input: exp = "{{{"
Output: Can't be made balanced using reversals
Example 3:-
Input: exp = "{{{{"
Output: 2
Example 4:-
Input: exp = "{{{{}}"
Output: 1
Example 5:-
Input: exp = "}{{}}{{{"
Output: 3
Github Link for Python Code:- github.com/net...
Please Like, Comment and Share the Video among your friends.
Also, Subscribe if you haven't already! :)
#Stack #MinBracket #Reversal #Python
Hi, we have 3 open and 1 close bracket in first example
yaa exactly she has done with 4 but it's 3/2 +1/2 and celing so ans is ceil(1) + ceil(0.5) = 1 + 1 = 2
@@nakulamate3558 answer would be 3
answer would be , 3/2 +1/2 and celing so ans is ceil(1.5) + ceil(0.5) = 2 + 1 = 3
Thanks a lot. I am making headway with data structures and algos. Be blessed.
Sounds great!
Hey can anyone please tell me the intuition behind calculating the ceiling of opening and closing brackets?
Wrong explanation, though idea is right, final result would be, open- 1 and close-1 , thus ceil[1/2] + ceil [1/2]=2 and so need 2 operation to make it valid
Only the first example, second example is right
hello ma'am how to work with this type of test case
Input
}{
{}{}{}
{{{}
---
Output
1. 2
2. 0
3. 1
Please make more videos on graph 🙏
You are great
Hi, i dont understand the why ceil works here.. :(
instead of liking you could have explained it
After balancing..If only open braces are left... This code won't work
Tooo Good :)
Thanks 😊
wrong explanation