Minimum Number of Swaps to Make String Balanced - Leetcode 1963 Weekly Contest - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025

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

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

    I have coded for 8+ years now and I still like watching these videos

    • @RN-jo8zt
      @RN-jo8zt ปีที่แล้ว +2

      me too ,because we don't do this stuff in our daily work

    • @saranshthukral4021
      @saranshthukral4021 3 หลายเดือนก่อน

      goat

  • @YT.Nikolay
    @YT.Nikolay 2 ปีที่แล้ว +49

    I am super happy I solved myself, and did it slightly differently. Basically, I count opened and closed, and when closed > opened, I do a swap AND decrease closed AND increase opened counters. Accepted! =)
    def minSwaps(self, s: str) -> int:
    opened, closed = 0, 0
    swaps = 0
    for bracket in s:
    if bracket == '[':
    opened += 1
    else:
    closed += 1
    if closed > opened:
    swaps += 1
    opened += 1
    closed -= 1
    return swaps

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

      Thank you!

    • @de-grafthazard9081
      @de-grafthazard9081 ปีที่แล้ว

      Brilliant

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

      To be honest your explanation even more intuitive. Thanks!

    • @scoobyy3112
      @scoobyy3112 3 หลายเดือนก่อน

      This is such a good solution

    • @mkycto
      @mkycto 3 หลายเดือนก่อน

      I thought about that approach, but i cant't physically explain on case with s = "]]][[["

  • @frida8519
    @frida8519 ปีที่แล้ว +11

    I just did this:
    class Solution:
    def minSwaps(self, s: str) -> int:
    flips, cSum = 0, 0;
    for b in s:
    if b == '[':
    cSum += 1;
    else:
    cSum -= 1;
    if cSum < 0:
    flips += 1;
    cSum = 1;
    return flips;
    Basically, if we ever have a negative sum, it means we have more closed brackets than we do open, so we have to make a swap and turn the ] into [, which means the current sum will be 1 again.

    • @nitiketshinde1458
      @nitiketshinde1458 9 หลายเดือนก่อน

      even I went with same way initially, just the thing is that it goes few ms slower than the another way which is given in video.

  • @user-fp4dr1ne7z
    @user-fp4dr1ne7z 2 ปีที่แล้ว +24

    A good question for a contest but a bad one for an interview. It such a difficult solution to come to on your own.

  • @Abhishek-xr1xw
    @Abhishek-xr1xw 3 หลายเดือนก่อน +2

    I love the way he come up with the solution.

  • @IAmIndraTheGod
    @IAmIndraTheGod 3 หลายเดือนก่อน +1

    The simple intuitiion for this problem is that,
    1. The number of open and closed brackets are same - given
    2. They are just out of order, so we need to swap them - given
    3. So how would you make it balanced, ?
    4. It will become balanced once you swap the ones that are unbalanced.
    5. So find out the brackets that are unbalanced, so we count closing brackets that dont have an associated open bracket.
    6. We keep track of the maximum number of such closing brackets
    7. since each swap would fix 2 of those closing brackets, we divide it by 2.

  • @rezaurrahman6455
    @rezaurrahman6455 9 วันที่ผ่านมา

    i love the way you explain. thanks for all your efforts.

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

    I am confusing why u did divide instead of minus since u said every swap remove 2. what I know so far is every swap will reduce by two because it will turn to be balanced and only left the unbalance one to swap/solve. For example, 3 -> 1 could be achieved by minus 2 why divide by two. i am stucking at there. pls help

    • @randomfool
      @randomfool ปีที่แล้ว +1

      its the same thing bro, 100/2 = 50 means for every operation 100 is reduced by 2 so there are 50 such operations

  • @MANOJKUMAR-mb2uw
    @MANOJKUMAR-mb2uw 2 ปีที่แล้ว +3

    Why only adding +1 to closing brackets why not vice versa i tried it but showing error

  • @bthulasikrishna9597
    @bthulasikrishna9597 3 หลายเดือนก่อน +2

    Why increment by one?
    If we have odd number of unmatched, then
    (unmatched+1)//2
    alternative is math.ceil(unbalanced/2)

    • @adityaonytube9860
      @adityaonytube9860 3 หลายเดือนก่อน +1

      works for even number of unmatched too right? like for eg. " ] ] [ [ " requries only 1 which is ((maxClose = 2) + 1)/2 ? Tho math.ceil works too

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

    Great explanation 👍👍👍

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

    Amazing. I could have never thought of it... How to come up with approaches on our own?

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

      Keep practising

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

    i think theres people who can come up like this solution. Even after i solved nearly 300 leetcode and watched most of the tutorial of the videos, i could only start to solve the problem with a brute force, cannot come up with the optimized solution ever 😮‍💨

    • @sankhadip_roy
      @sankhadip_roy 6 หลายเดือนก่อน

      What is your current status?

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

    but why increment by 1?

    • @sanskartiwari2496
      @sanskartiwari2496 2 ปีที่แล้ว +1

      I think of it as (maxClose //2) + (maxClose%2)

    • @bthulasikrishna9597
      @bthulasikrishna9597 3 หลายเดือนก่อน

      If we have odd number of unmatched, alternative is math.ceil(unbalanced/2)

  • @杨熙-v6v
    @杨熙-v6v 10 หลายเดือนก่อน

    amazing!hope to solve neetcode all by following the series of video!

  • @AustinWeeks
    @AustinWeeks 3 หลายเดือนก่อน +1

    This one took years off my life

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

    Best Explanation so far

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

    do we really need maxClose in the code? It is always 0 and I think it's enough to do (close + 1 ) / 2.

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

      actually he did not explain need of maxClose. For test case ][][][, your logic will fail

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

      When you reach the end, close will be 0. So you need another variable

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

    amazing explanation

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

    Thanks but can you please elaborate why did we use max variable ?

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

      to keep track of max of closecount at an instance

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

      if we do not check the max variable, we may meet ]]][[[ and reach 0 in the end

  • @WebDevAnjali
    @WebDevAnjali 3 หลายเดือนก่อน

    I CAN NEVER THINK OF THIS SOLUTION EVEN WITHOUT THE ALGO ....I SPENT 2 HOURS BUT COULDN'T THINK OF RESOLVING ALL TEST CASE IT WILL GET STUCK IN ONE OR ANOTHER CASE ....

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

    How did you come up with that?
    great solution 🤟

  • @Live-hh6li
    @Live-hh6li 3 ปีที่แล้ว

    Best explanation

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

    good work.

  • @sharma01ketan
    @sharma01ketan ปีที่แล้ว

    its quite similar to Minimum Add to Make Parentheses Valid

  • @aspiring_Sr.dev_Om
    @aspiring_Sr.dev_Om 4 หลายเดือนก่อน

    Spoiler @ 4:32 MaxSwaps

  • @AymenDaoudi-u6h
    @AymenDaoudi-u6h ปีที่แล้ว +1

    It's wrong on many levels to ask such a question in an interview

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

    very nice.

  • @johnj171
    @johnj171 7 หลายเดือนก่อน

    youu broo love you

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

    Solve 0-1 knapsack, other tutorials on youtube are not that clear

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

      watch aditya verma dp series

  • @nmkcomps
    @nmkcomps ปีที่แล้ว

    What happens if I just take open brackets. Won’t your solution return 0

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

    this code doesn't work for multiple testcases

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

      sorry, you must have made a mistake. this exact code worked for me.

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

    class Solution:
    def minSwaps(self, s: str) -> int:
    stack = []

    for ch in s:
    if ch == '[':
    stack.append(ch)
    elif stack:
    stack.pop()

    return (len(stack)+1)//2



    #approach_2:
    class Solution:
    def minSwaps(self, s: str) -> int:
    size = 0

    for char in s:
    if char == '[': size += 1
    elif size != 0 : size -= 1

    return (size+1)//2

  • @aisharawat9102
    @aisharawat9102 ปีที่แล้ว

    Mera dimaag kb chlega🥺🥺

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

    I am afraid of Leetcode Weekly Contests because I don't know, I can solve it or not or maybe giveup in half-way. But I like the problems in leetcode and all other platforms. Don't judge me, I am an average human. Can anyone help me in this?

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

      @@BhanuTejaPogiri-rj3qp Thank you!

    • @WebDevAnjali
      @WebDevAnjali 3 หลายเดือนก่อน

      i used to think of myself as an average but since i've tried leetcode i come to know that i'm way below the bottom line 🙂i can't think of logics .... all the concepts of dp , recursive, tree or problems of knapsack or greedy approch i used to think of them in graphical way imagining in my head but during my academics i never even know those stuffs can be coded i hell have to idea how.... even being the easiest problem i get stuck quite often ....i wonder is it only me this stupid or there exists ppl ...i dont know how long will it take me to grab the problem solving flow or from where to begin with😭😭😭

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

    noice

  • @sabukuna
    @sabukuna หลายเดือนก่อน

    annoying problem

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

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

    Nice solution, can you please do video on 1055. Shortest Way to Form String from leetcode, I think there are multiple solutions possible for it.