Minimum Number of Flips to make Binary String Alternating - Sliding Window - Leetcode 1888 - Python

  Рет қаралды 44,336

NeetCode

NeetCode

Күн бұрын

Пікірлер: 57
@theghostwhowalk
@theghostwhowalk 2 жыл бұрын
Great explanation. This should be a hard problem. Even brute force is not quite intuitive in interview setup without hints.
@aybarskeremtaskan
@aybarskeremtaskan 2 жыл бұрын
Hi, thanks for the explanation. I think the reason that extending the alternating strings (alt1 and alt2) work is that each time they transform into each other. (0101... becomes 1010... and vice versa etc.) which means that we should do diff2 = diff-1 and diff1 = diff2 -1 instead of diff -=1 and diff2-=1. The solution works because at each round we take minimum of diff1 and diff2 which makes swapping diff1 and diff2 unnecessary computation wise (even though logically it seems wrong).
@dheepthaaanand3214
@dheepthaaanand3214 20 күн бұрын
Thank you for this! Yes, when we are considering 110001, we would want to compare it with 010101 as well, but that comes by shifting 101010, the 2nd possible alternation. And so the diff is technically 4 becoming 2 as we should compare diff with the same alternation
@stefan.musarra
@stefan.musarra 6 ай бұрын
My key take-away from this problem is that shifting elements from the beginning to the end (or the opposite) is the same as a sliding window on the string doubled. Thanks for showing us that brilliant observation/trick. In this problem, if the string has an even length, you do not need to slide the window at all, but it essential to slide the window if the string has an odd length. Example: "11100" needs to be shifted 4 times but requires only 1 flip.
@madhukiranattivilli2321
@madhukiranattivilli2321 Жыл бұрын
This O(n) algo can be slightly improved with these changes : Essentially, use bit manipulation operations (1) No need to have an O(n) loop to build the alternating bits mask. Bit manipualation methods and geometric progression math formula can be used to get the mask bit string in O(1) time (2) No need to have an O(n) loop to count bit flips. XOR of the input bit string segment and mask bit string segment can be done to get few 1 bits representing bit flips. Segments can be obtained by using an all 1s mask, AND with the input and alternating bits mask to get the segments, and right shift the all 1s mask to get the next segments. Later, the 1s in the XORed result can be obtained by using bit manipulation methods and geometric progression math formula -- build as many cloned blocks as is the length of the XORed result, AND each block with 1, 10, 100, ..., 1000 mask segments -- i.e. AND the 1st big block and the 2nd mask block -- right shift the ANDed block k times (where k is the length of the XORed block) to get all "bit flip indicator" bits (0 or 1) to the rightmost bit position All these can be done mathematically. No need to code. Math equation (geometric progression) gives the result in O(1) Thus the final program would be a much closer to O(n) Makes sense? (I've implemented this)
@amandubey5287
@amandubey5287 2 жыл бұрын
Thankyou so much neetcode, I was scared of this question until this video. Thankyou so much for explaining the logic with such details.
@DroidHolicOfficial
@DroidHolicOfficial 2 жыл бұрын
Beautiful Explanation man! One thing we can do to reduce the space is to double the string only when the given string is of odd length. I noticed that for even strings, doubling is of no use. i.e., type-1 operation is of no use at all. Only for odd strings we may get a minimum flip value by doing some type-1 operations. s = s if k % 2 == 0 else s + s
@Chirayu19
@Chirayu19 Жыл бұрын
I also thought the same, Nice!
@akhilkhubchandani2632
@akhilkhubchandani2632 3 жыл бұрын
Thanks for this , keep posting every contest . Keeps me motivated
@mohaimenchowdhury
@mohaimenchowdhury Жыл бұрын
It is great that you mentioned you have come up with that idea of type 1 operation from the leetcode discuss. It motivates people more that even NeetCodeIO looks for the leetcode discuss section, so why would we feel down to check the discuss section?
@deathbombs
@deathbombs 2 жыл бұрын
The key was seeing we can find answers by converting to a "target string", I thought there was some other trick to achieve alternating pattern, like checking for sequences
@hackytech7494
@hackytech7494 3 жыл бұрын
Thankyou so much..... Best explanation ever. Please keep posting for every contest 🙏
@ducthinh2412
@ducthinh2412 2 жыл бұрын
Great explanation! Just curious, were you able to solve it during the contest, or you spent time afterwards to come up with the solution?
@maheswarreddy3917
@maheswarreddy3917 3 жыл бұрын
Clean explanation man thanks. Struggling to understand from leetcode discuss section.
@alexgrossbard6206
@alexgrossbard6206 Жыл бұрын
It is not necessary to check if left - right + 1 == n: res = min(res, diff1, diff2). You can do it at the bottom of the if left - right + 1 > n block since left will have been updated and that will make left - right + 1 == n
@udemezueiloabachie8626
@udemezueiloabachie8626 6 ай бұрын
I always watch your explanation then I code my solution before wathcing your code solution.
@upendsingh9263
@upendsingh9263 4 ай бұрын
to generate alt1 and alt2 we can use alt1 = "01" * n alt2 = "10" * n
@ehsanmon
@ehsanmon 2 жыл бұрын
Such a smart solution. Is this really a medium question though? It's pretty hard to come up with such a solution during an interview.
@amogchandrashekar8159
@amogchandrashekar8159 3 жыл бұрын
Thanks for this, I was about to request this in comments!
@NeetCode
@NeetCode 3 жыл бұрын
No problem!
@neeldesai108
@neeldesai108 2 жыл бұрын
Just FYI, Same implementation in Java returns TLE Thanks, ndesai
@RanjuRao
@RanjuRao 2 жыл бұрын
Even I got TLE in java
@AymenDaoudi-u6h
@AymenDaoudi-u6h Жыл бұрын
Similar, in C#
@rajasbaadkar4989
@rajasbaadkar4989 6 ай бұрын
Use StringBuilder for alt1 and alt2
@arnabpersonal6729
@arnabpersonal6729 3 жыл бұрын
we can get alt values by using '01'*n and '10'*n
@siddharthsinghchauhan8664
@siddharthsinghchauhan8664 2 жыл бұрын
Aren't the string concatenation O(n^2) as they are immutable...
@KaKaphoebe
@KaKaphoebe 3 жыл бұрын
very clear video. I like it. Thank you!
@Ruslanpv6bs
@Ruslanpv6bs 6 ай бұрын
Super beatiful solution!
@sidazhong2019
@sidazhong2019 Жыл бұрын
if (r-l+1)==n: res = min(res,diff1,diff2) This if condition is unnecessary, since the left pointer has already been increment. it's just like if (10-0+1>10), again if (10-1+1)==10 in the same loop. so completly waste. you can just put " res = min(res,diff1,diff2) " inside of the prev if condition.
@abukhandaker7558
@abukhandaker7558 6 ай бұрын
We will watch the problem or the ads? Seems like greedy algorithm
@levimatheri7682
@levimatheri7682 5 ай бұрын
Underrated comment 😂😂
@albertlee5648
@albertlee5648 Жыл бұрын
Thanks for the video, but I think there is a mistake in the code. The string concatenation creates new copies every time, so the time complexity of constructing alt1 and alt2 is O(n^2). I think the list operation should be used instead
@ayushman_sinhaa
@ayushman_sinhaa 2 жыл бұрын
Thank you so much! I Finally understood the problem
@ZQutui
@ZQutui 3 жыл бұрын
Thanks for keeping up posting new solutions. Btw, what do you do as a software engineer? Web, Mobile, backend, or something else?
@AbhishekJaiswal24
@AbhishekJaiswal24 2 жыл бұрын
He is completely anonymous for us , he works at Google
@crit19871
@crit19871 3 жыл бұрын
Thanks for your content. Your solutions are amazing. Could you please make a video on Subarray sum equals K.
@kanehtube5390
@kanehtube5390 8 ай бұрын
ur videos are such a pretty ones.
@hieijaganshi7088
@hieijaganshi7088 7 ай бұрын
I don't understand why its impossible to get a better solution by performing type-1 operations, then type-2 operations, then type-1 operations (etc) VS just performing all the type-1 operations and then all the type-2 operations
@sairam3351
@sairam3351 Жыл бұрын
Thank you so much bro.
@0088abs
@0088abs Жыл бұрын
Nice way to explain , but I think this is not the optimal solution and errors out for a test case having around 74461 characters in input . Error- :Time limit exceeded" . Kindly update the code with the upgraded version if any . It would be really helpful if there can be a direct link for the source code as well . Thanks, your videos are helpful !!!
@akashnarayan83
@akashnarayan83 3 жыл бұрын
Thank you
@b_1729-j8j
@b_1729-j8j 3 жыл бұрын
Thanks dude
@joeycopperson
@joeycopperson 3 жыл бұрын
Does anyone know why string is added to original only once? How does duplicating string solve the problem? Is it coz string comprise of '0' and '1' (only 2 types of digits) ?
@siddharth4069
@siddharth4069 2 жыл бұрын
Cause its a cyclic problem. You are allowed to remove characters from the front of the string and attach to the back. Thus this will lead to the possibility of multiple different strings from a single string of length "n". If we attach the string to itself and then look at all "n" length substrings of the s, we can look at all possibilities of strings that we could have formed by removing characters from the front of the string and attaching to the end.
@victoriac7257
@victoriac7257 3 жыл бұрын
Hey can you please do 1889 as well? Thanks!
@yogendrakesharwani3650
@yogendrakesharwani3650 7 ай бұрын
This is not medium level question it very very hard for anyone to come up with this type of solution by it's own
@ankithreddyadudodla7673
@ankithreddyadudodla7673 3 жыл бұрын
nice explanation!!!
@abhinavsinghkushwah2416
@abhinavsinghkushwah2416 3 жыл бұрын
Thanks!
@vijaykumarlokhande1607
@vijaykumarlokhande1607 3 жыл бұрын
keep posting.
@AymenDaoudi-u6h
@AymenDaoudi-u6h Жыл бұрын
Efficient ?! it shows 1232ms right there, and it LTEs in other languages. But good explanation thank you.
@Sulerhy
@Sulerhy 7 ай бұрын
double size of list -> too brilliant. I know the solution, but I can't figure it out how to code it effectively.
@ygwg6145
@ygwg6145 Жыл бұрын
Actually diff1 + diff2 == L. Only one diff/alt is needed.
@SmoothCode
@SmoothCode 2 жыл бұрын
What is the purpose of s = s+ s ? What does it help us with?
@siddharth4069
@siddharth4069 2 жыл бұрын
Cause its a cyclic problem. You are allowed to remove characters from the front of the string and attach to the back. Thus this will lead to the possibility of multiple different strings from a single string of length "n". If we attach the string to itself and then look at all "n" length substrings of the s, we can look at all possibilities of strings that we could have formed by removing characters from the front of the string and attaching to the end.
@34_harshdeepraghuwanshi98
@34_harshdeepraghuwanshi98 3 жыл бұрын
Nice
@khushboosharma1085
@khushboosharma1085 3 жыл бұрын
👍
Long Nails 💅🏻 #shorts
00:50
Mr DegrEE
Рет қаралды 16 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 31 МЛН
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 5 МЛН
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 2,1 МЛН
10 Crazy Python Operators That I Rarely Use
11:37
Indently
Рет қаралды 43 М.
Find Kth Bit in Nth Binary String - Leetcode 1545 - Python
22:27
How I Failed the Google Coding Interview (and lessons I learned)
14:24
Permutation in String - Leetcode 567 - Python
19:40
NeetCode
Рет қаралды 241 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 835 М.
you will never ask about pointers again after watching this video
8:03
Sliding Window Technique - Algorithmic Mental Models
36:45
Ryan Schachte
Рет қаралды 363 М.
Telefonu Parçaladım!🤯
0:18
Safak Novruz
Рет қаралды 39 М.
👍🏻 Samsung Galaxy A56 - ЕГО ЗАХОТЯТ ВСЕ! Xiaomi так не сможет…
12:05
Thebox - о технике и гаджетах
Рет қаралды 240 М.