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

ความคิดเห็น • 18

  • @ksansudeen
    @ksansudeen 4 ปีที่แล้ว +4

    Hi, we have 3 open and 1 close bracket in first example

    • @nakulamate3558
      @nakulamate3558 4 ปีที่แล้ว

      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

    • @sush9889
      @sush9889 4 ปีที่แล้ว

      @@nakulamate3558 answer would be 3

    • @bunnyrabits
      @bunnyrabits 3 ปีที่แล้ว +3

      answer would be , 3/2 +1/2 and celing so ans is ceil(1.5) + ceil(0.5) = 2 + 1 = 3

  • @homeOfTrivia
    @homeOfTrivia 3 ปีที่แล้ว

    Thanks a lot. I am making headway with data structures and algos. Be blessed.

  • @imranwahid9871
    @imranwahid9871 3 ปีที่แล้ว +2

    Hey can anyone please tell me the intuition behind calculating the ceiling of opening and closing brackets?

  • @AbhishekKumar-ck9es
    @AbhishekKumar-ck9es 3 ปีที่แล้ว +2

    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

  • @meghapatil2249
    @meghapatil2249 2 ปีที่แล้ว

    hello ma'am how to work with this type of test case
    Input
    }{
    {}{}{}
    {{{}
    ---
    Output
    1. 2
    2. 0
    3. 1

  • @Niranjan-dt2mc
    @Niranjan-dt2mc 3 ปีที่แล้ว

    Please make more videos on graph 🙏

  • @Aryansingh-fk7hy
    @Aryansingh-fk7hy 3 ปีที่แล้ว

    You are great

  • @ksansudeen
    @ksansudeen 4 ปีที่แล้ว

    Hi, i dont understand the why ceil works here.. :(

    • @MohitYadav-nu4fu
      @MohitYadav-nu4fu 2 ปีที่แล้ว

      instead of liking you could have explained it

  • @xmen2794
    @xmen2794 4 ปีที่แล้ว

    After balancing..If only open braces are left... This code won't work

  • @jah7120
    @jah7120 4 ปีที่แล้ว

    Tooo Good :)

  • @akashkumarsingh5355
    @akashkumarsingh5355 3 ปีที่แล้ว

    wrong explanation