Minimum Swaps to Group All 1's Together II - Leetcode 2134 - Python

  Рет қаралды 14,935

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 49
@peacefulcat3578
@peacefulcat3578 Ай бұрын
0:57 In the example "0101101", you actually only need 1 swap to make "0001111" or "0111100".
@tuandino6990
@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
@jayjw1 Ай бұрын
nah its not you, this problem is a bit evil for the beginning of the month lmao
@blackbeard3449
@blackbeard3449 Ай бұрын
I also thought so. 😢
@swarajandhale4088
@swarajandhale4088 Ай бұрын
Same here man
@akshatharlalka5334
@akshatharlalka5334 Ай бұрын
Not one hours for me but a good 20 mins.
@finemechanic
@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
@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
@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
@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
@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
@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
@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
@sundew_ii Ай бұрын
this one was tough :p
@MacintoshPlus1MB
@MacintoshPlus1MB 16 күн бұрын
this is one of the greatest explanations ever
@pastori2672
@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
@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
@rliu2 Ай бұрын
and you just take the min of the two ans
@adityamaharshi2320
@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
@AKProductionsTelugu Ай бұрын
It felt to easy yet amazing after watching the first 3 minutes of your video ❤❤ i was able to solve it
@ganeshjaggineni4097
@ganeshjaggineni4097 Ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@jamestwosheep
@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
@peacefulcat3578 Ай бұрын
I was staring at this problem for 10 mins. Thinking about the intervals, and how to calculate average distance between dots......
@galkk3
@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
@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
@vishaalkumaranandan2894 Ай бұрын
The % idea was really brilliant
@gmh14
@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
@Codisrocks Ай бұрын
Another little tweak, you only have to check the max when you add another one at your right pointer.
@chien-yuyeh9386
@chien-yuyeh9386 Ай бұрын
Thanks for sharing!
@MaheshKumar-mh9mj
@MaheshKumar-mh9mj Ай бұрын
in line 7 we can just got til n+totalOnes . 2*n is not required
@adamtawfik6337
@adamtawfik6337 Ай бұрын
Thank you
@user-sx7zc3ez4w
@user-sx7zc3ez4w Ай бұрын
Everyday thank you
@haidang2452
@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
@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
@Badrsh-tk4cd Ай бұрын
Can you solve the ready-made function in the list? Or is it forbidden?
@GryffindorKnowledgeSeeker
@GryffindorKnowledgeSeeker Ай бұрын
How'd you know this solution would work?
@mohamedsayed8697
@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
@ajitpalsingh606 Ай бұрын
Coded on my own
@strangerlois7307
@strangerlois7307 Ай бұрын
Why we need window one, max_window_ones?
@kapilkhandelwal48
@kapilkhandelwal48 Ай бұрын
I thought it would be a cakewalk
@raviyadav2552
@raviyadav2552 Ай бұрын
I couldn't even solve it using brute force and wasted an hour
@riddles4013
@riddles4013 Ай бұрын
4:12 🤔🤔
@SonuKumar-q5o1b
@SonuKumar-q5o1b Ай бұрын
why neetcode website not able to open??????? i am facing issue from last 3-4 days
@v0nnyboy
@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
@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
@avinavkashyap8802 Ай бұрын
@neeetcode where to apply for the interview
@fabricio5p
@fabricio5p Ай бұрын
I hate "guess the trick" questions
@qulinxao
@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
@hasferrr
@hasferrr Ай бұрын
i hate circular array
Секрет фокусника! #shorts
00:15
Роман Magic
Рет қаралды 98 МЛН
Or is Harriet Quinn good? #cosplay#joker #Harriet Quinn
00:20
佐助与鸣人
Рет қаралды 53 МЛН
Фейковый воришка 😂
00:51
КАРЕНА МАКАРЕНА
Рет қаралды 7 МЛН
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 212 М.
Reacting to Controversial Opinions of Software Engineers
9:18
Fireship
Рет қаралды 2,1 МЛН
Leetcode 46. Permutations : Introduction to backtracking
10:06
ComputerBread
Рет қаралды 95 М.
OpenAI o1 is Better Than I Expected
17:11
NeetCodeIO
Рет қаралды 81 М.
How I Approach a New Leetcode Problem (live problem solving)
25:31
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 221 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 302 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 140 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 508 М.
Ground Reality of Tier 3 colleges is Horrifying
12:13
Harkirat Singh
Рет қаралды 200 М.