0:57 In the example "0101101", you actually only need 1 swap to make "0001111" or "0111100".
@tuandino6990Ай бұрын
My dumb ass brain stopped functioning and thought we can only do swap between two adjacent number and waste 1 hour on this problem...
@jayjw1Ай бұрын
nah its not you, this problem is a bit evil for the beginning of the month lmao
@blackbeard3449Ай бұрын
I also thought so. 😢
@swarajandhale4088Ай бұрын
Same here man
@akshatharlalka5334Ай бұрын
Not one hours for me but a good 20 mins.
@finemechanicАй бұрын
You can go from 2*N iterations to N + total_ones: calculate ones in the window nums[:total_ones] and then move your window of constant size from 0 to N-1.
@mudit7394Ай бұрын
holy shit this is so easy when you explained it lol. I tried the greedy approach and it worked until it failed for the test case lol ur approach is amazin
@arnavchauhan9637Ай бұрын
great explanation sir! Fun Fact: we can skip the if statement to add 1 as the array only contains 1 and 0 so if we blindly take sum+=arr[i] it will work same i guess :/ but anyways what a great guy!!
@md_pedia1Ай бұрын
Another approach: (using sliding windows ofc) total_ones=nums.count(1) l=0 nums.extend(nums[:total_ones-1]) zero_cnt=0 res=float("inf") for r in range(len(nums)): zero_cnt+=not(nums[r]) if (r-l+1)==total_ones: res=min(res,zero_cnt) zero_cnt-=not(nums[l]) l+=1 return res if res!=float("inf") else 0
@qulinxaoАй бұрын
l,f=len(n),sum(n); o=t=sum(n[:f]) for i,v in enumerate(n,f): o=max(o,t:=t+n[i%l]-v) return f-o
@michaelroditis1952Ай бұрын
the left pointer needs to go only up to N-1 the for loop should be this: for r in range(N+total_ones) That way the "l" doesn't need mod, and it's faster for large N and small total_ones
@qulinxaoАй бұрын
l,f=len(n),sum(n); o=t=sum(n[:f]) for i,v in enumerate(n,f): o=max(o,t:=t+n[i%l]-v) return f-o
@sundew_iiАй бұрын
this one was tough :p
@MacintoshPlus1MB16 күн бұрын
this is one of the greatest explanations ever
@pastori2672Ай бұрын
the hints of this of this problem are pretty good they gave like 5 hints and the second hint gives it all away
@rliu2Ай бұрын
i think a better idea would be realizing that by grouping 1s tgt, 0s would be group tgt too, so you just keep track of cnt of 0s for the respective 0 wnd too.
@rliu2Ай бұрын
and you just take the min of the two ans
@adityamaharshi2320Ай бұрын
For the circular part I tries consolidating zeros instead of ones , since if zeros are linearly together , ones also must be together , circular or otherwise.
@AKProductionsTeluguАй бұрын
It felt to easy yet amazing after watching the first 3 minutes of your video ❤❤ i was able to solve it
@ganeshjaggineni4097Ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@jamestwosheepАй бұрын
You probably could've made your run time a little more quicker if you exited the loops once the left pointer exceeds the length of the original array instead of going the full 2N. But yeah, this one was a doozy - I don't think I would have gotten it without the hints that they gave.
@peacefulcat3578Ай бұрын
I was staring at this problem for 10 mins. Thinking about the intervals, and how to calculate average distance between dots......
@galkk3Ай бұрын
my solution, more consice and the loop is running N + # of 1's: def minSwaps(self, nums: List[int]) -> int: total = sum(nums) right = total if right == len(nums): return 0 maxSub = sum(nums[:total]) window = maxSub for left in range(len(nums)): window = window - nums[left] + nums[right] maxSub = max(maxSub, window) right = (right + 1) % len(nums) return total - maxSub
@qulinxaoАй бұрын
l,f=len(n),sum(n); o=t=sum(n[:f]) for i,v in enumerate(n,f): o=max(o,t:=t+n[i%l]-v) return f-o
@vishaalkumaranandan2894Ай бұрын
The % idea was really brilliant
@gmh14Ай бұрын
The reason why we never saw a Minimum Swaps to Group All 1's Together I is because it is premium-only. i guess it's the same problem without circular edge case
@CodisrocksАй бұрын
Another little tweak, you only have to check the max when you add another one at your right pointer.
@chien-yuyeh9386Ай бұрын
Thanks for sharing!
@MaheshKumar-mh9mjАй бұрын
in line 7 we can just got til n+totalOnes . 2*n is not required
@adamtawfik6337Ай бұрын
Thank you
@user-sx7zc3ez4wАй бұрын
Everyday thank you
@haidang2452Ай бұрын
Thank you for sharing. You can apply Sliding Window for counting minimum 0 in K window def minSwaps(self, nums: List[int]) -> int: """ Calculate minimum sliding window for 0 """ k = nums.count(1) numZero = nums[:k].count(0) ans = numZero n = len(nums) for R in range(k, 2*n): if nums[R%n] == 0: numZero +=1 if nums[(R-k)%n] == 0: numZero -= 1 ans = min(ans, numZero) return ans
@qulinxaoАй бұрын
l,f=len(n),sum(n); o=t=sum(n[:f]) for i,v in enumerate(n,f): o=max(o,t:=t+n[i%l]-v) return f-o
@Badrsh-tk4cdАй бұрын
Can you solve the ready-made function in the list? Or is it forbidden?
@GryffindorKnowledgeSeekerАй бұрын
How'd you know this solution would work?
@mohamedsayed8697Ай бұрын
You consider it against all possible cases. You devise cases and say to yourself that this solution must return this valid value if it works properly and if it don't, then something is wrong with the solution (using counter-examples).
@ajitpalsingh606Ай бұрын
Coded on my own
@strangerlois7307Ай бұрын
Why we need window one, max_window_ones?
@kapilkhandelwal48Ай бұрын
I thought it would be a cakewalk
@raviyadav2552Ай бұрын
I couldn't even solve it using brute force and wasted an hour
@riddles4013Ай бұрын
4:12 🤔🤔
@SonuKumar-q5o1bАй бұрын
why neetcode website not able to open??????? i am facing issue from last 3-4 days
@v0nnyboyАй бұрын
surprisingly ... i did not find this tough at all ! Simple sliding window.. with double the array size ! easy def minSwaps(self, nums: List[int]) -> int: ones = nums.count(1) double = nums+nums ct = nums[:ones].count(1) maxx = ct l=0 for i in range(ones,len(double)): if double[i]==1: ct+=1 if double[l]==1: ct-=1 maxx = max(ct,maxx) l+=1 return ones - maxx
@qulinxaoАй бұрын
l,f=len(n),sum(n); o=t=sum(n[:f]) for i,v in enumerate(n,f): o=max(o,t:=t+n[i%l]-v) return f-o
@avinavkashyap8802Ай бұрын
@neeetcode where to apply for the interview
@fabricio5pАй бұрын
I hate "guess the trick" questions
@qulinxaoАй бұрын
l,f=len(n),sum(n); o=t=sum(n[:f]) for i,v in enumerate(n,f): o=max(o,t:=t+n[i%l]-v) return f-o