I have coded for 8+ years now and I still like watching these videos
@RN-jo8zt Жыл бұрын
me too ,because we don't do this stuff in our daily work
@YT.Nikolay2 жыл бұрын
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
@SkyeTian2 жыл бұрын
Thank you!
@de-grafthazard9081 Жыл бұрын
Brilliant
@ibragim1989 Жыл бұрын
To be honest your explanation even more intuitive. Thanks!
@frida8519 Жыл бұрын
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.
@nitiketshinde14585 ай бұрын
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 Жыл бұрын
A good question for a contest but a bad one for an interview. It such a difficult solution to come to on your own.
@sketchwithbratati43973 жыл бұрын
Amazing. I could have never thought of it... How to come up with approaches on our own?
@MGtvMusic3 жыл бұрын
Keep practising
@杨熙-v6v6 ай бұрын
amazing!hope to solve neetcode all by following the series of video!
@MANOJKUMAR-mb2uw2 жыл бұрын
Why only adding +1 to closing brackets why not vice versa i tried it but showing error
@jakjun40772 жыл бұрын
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 Жыл бұрын
its the same thing bro, 100/2 = 50 means for every operation 100 is reduced by 2 so there are 50 such operations
@AustinWeeks4 күн бұрын
This one took years off my life
@ocoocososococosooooosoococoso10 ай бұрын
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_roy2 ай бұрын
What is your current status?
@shantanukumar40812 жыл бұрын
Great explanation 👍👍👍
@shivangagrawal29232 жыл бұрын
Best Explanation so far
@ashwinvarma93493 жыл бұрын
but why increment by 1?
@sanskartiwari2496 Жыл бұрын
I think of it as (maxClose //2) + (maxClose%2)
@rounakbhatia1212 жыл бұрын
amazing explanation
@akankshasharma7498 Жыл бұрын
How did you come up with that? great solution 🤟
@shubhamdeshmukh92873 жыл бұрын
good work.
@ketansharma6955 Жыл бұрын
its quite similar to Minimum Add to Make Parentheses Valid
@AymenDaoudi-u6h11 ай бұрын
It's wrong on many levels to ask such a question in an interview
@Live-hh6li3 жыл бұрын
Best explanation
@johnj1713 ай бұрын
youu broo love you
@sainikhil9562 жыл бұрын
Thanks but can you please elaborate why did we use max variable ?
@rohitbk79202 жыл бұрын
to keep track of max of closecount at an instance
@chenyixiang2424 Жыл бұрын
if we do not check the max variable, we may meet ]]][[[ and reach 0 in the end
@shubhamdeshmukh92873 жыл бұрын
very nice.
@alisherkholmirzaev78852 жыл бұрын
do we really need maxClose in the code? It is always 0 and I think it's enough to do (close + 1 ) / 2.
@TechEats2 жыл бұрын
actually he did not explain need of maxClose. For test case ][][][, your logic will fail
@ronifintech94342 жыл бұрын
When you reach the end, close will be 0. So you need another variable
@aspiring_Sr.dev_OmАй бұрын
Spoiler @ 4:32 MaxSwaps
@nmkcomps Жыл бұрын
What happens if I just take open brackets. Won’t your solution return 0
@soumikghosh15223 жыл бұрын
Solve 0-1 knapsack, other tutorials on youtube are not that clear
@kritika_02043 жыл бұрын
watch aditya verma dp series
@mdanishossain026 Жыл бұрын
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 Жыл бұрын
Mera dimaag kb chlega🥺🥺
@ssenthilnathan33 жыл бұрын
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?
@ssenthilnathan33 жыл бұрын
@@BhanuTejaPogiri-rj3qp Thank you!
@ytsh57522 жыл бұрын
noice
@apoorvakumar24103 жыл бұрын
this code doesn't work for multiple testcases
@NeetCode3 жыл бұрын
sorry, you must have made a mistake. this exact code worked for me.
@ameynaik27433 жыл бұрын
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.