Bitwise AND of Numbers Range - Leetcode 201 - Python

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

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 57
@yang5843
@yang5843 9 ай бұрын
Appreciate you for making this video, despite how difficult it is to explain the topic.
@yang5843
@yang5843 9 ай бұрын
The 2nd solution is so simple once you understand the bit manipulation!
@deepmondal8459
@deepmondal8459 9 ай бұрын
7:01 class Solution: def rangeBitwiseAnd(self, left: int, right: int) -> int: res = 0 for i in range(32): # Iterate over each bit position if (left >> i) & 1 and (right >> i) & 1: # Check if the i-th bit is set in both left and right mask = (1
@siddhr6241
@siddhr6241 5 ай бұрын
I feel this video is an exception to your usual easy to understand explanations. I think it would be more concise to explain that the left and right most number would have a common prefix. The problem reduces to finding that common prefix (if any) between the left and right numbers of length 32 (and put zeros to the remaining positions), giving O(1) time complexity.
@logchamption
@logchamption 9 ай бұрын
class Solution: def rangeBitwiseAnd(self, left: int, right: int) -> int: while left < right: right &= right - 1 return right
@jayraval2846
@jayraval2846 9 ай бұрын
Did the same thing
@BlunderArmour
@BlunderArmour 9 ай бұрын
Neato. The intuition here is God tier.
@meetverma4812
@meetverma4812 9 ай бұрын
My man If I ever Get into an MNC You will be the reason for it HATS OFF!!
@alphaferret1220
@alphaferret1220 9 ай бұрын
hello everyone doing problem of the day
@shepmax555
@shepmax555 Күн бұрын
I was expecting to see the 2nd solution 1st. It makes much more sense than the 1st one.
@jing_wang_jjang
@jing_wang_jjang 9 ай бұрын
Thanks for great solution! After I saw your answer, I immediately understood the problem.
@udaygodaba6860
@udaygodaba6860 9 ай бұрын
Simply Wow! what an approach
@Pegasus02Kr
@Pegasus02Kr 9 ай бұрын
I think the second solution is a million times more intuitive and easier to reason about
@ninjaasmoke
@ninjaasmoke 9 ай бұрын
class Solution { public: int rangeBitwiseAnd(int left, int right) { int c=0; while (left!=right) { left = left>>1; right = right>>1; ++c; } return left
@alnaskabeer1361
@alnaskabeer1361 9 ай бұрын
The way i approached it was different. I realized that in order for it to have an non 0 solution, the the number of digits in the binary rep should be equal, since otherwise in order to gain an extra digit, there must be a power of 2 within the range, which obviously makes the entire thing 0. I used log2 of both of them numbers to check the digits, and then i statted at that bit and found the prefix that is common... And then returned the answer.
@erminiottone
@erminiottone 9 ай бұрын
The second solution is MUCH simpler than the first one
@electricindro2236
@electricindro2236 9 ай бұрын
The 2nd solution was brilliant!
@karthikeyansundaram6894
@karthikeyansundaram6894 2 ай бұрын
Thank you very much.
@m.kamalali
@m.kamalali 9 ай бұрын
The second approach is awesome
@nicolasguillenc
@nicolasguillenc 9 ай бұрын
I will save this video for a time I feel smart enough to understand it 😂
@tusharkumarraj6066
@tusharkumarraj6066 2 ай бұрын
the explanation isnt good enough
@imantaheari
@imantaheari 9 ай бұрын
tnx for explaination that was perfect
@jaatharsh
@jaatharsh 9 ай бұрын
great explanation, loved it
@akshayavenkatesan2912
@akshayavenkatesan2912 9 ай бұрын
how is the time complexity log(n) ? Can someone please explain ?
@markerman491
@markerman491 9 ай бұрын
When is the more detailed version of the roadmap coming out? Just watched your video of saying you might do it, any updates?
@adiesha_
@adiesha_ 9 ай бұрын
I would argue this is a constant time algorithm, since all integers has 32 bits and this value is a constant. public int rangeBitwiseAnd(int left, int right) { int diff = right - left + 1; int digits = (int)Math.ceil((Math.log(diff)/Math.log(2))); int val = (0x7FFFFFFF
@thiagosdev
@thiagosdev 9 ай бұрын
That was really really clever
@gauravsharma-lu4if
@gauravsharma-lu4if 9 ай бұрын
thank you sooo much
@ALONEWILL
@ALONEWILL 9 ай бұрын
thanks for the video. what's your pen tablet? waccom? thanks
@GameFlife
@GameFlife 9 ай бұрын
Damn finally i can breath easily this problem is tough for me tks neet for trying to explain
@sri_harsha_dv
@sri_harsha_dv 9 ай бұрын
I tried O(n) method but it doesn't work. I found similar way of doing the 2nd method in the video. Obviously the method discussed in the video is the ask but I can't figure out in terms of bit operations. I know its not good but here is my solution. bin_left,bin_right = bin(left)[2:],bin(right)[2:] if len(bin_right) > len(bin_left): return 0 def recursion(str1,str2): if str1: if str1[0] == str2[0]: return str1[0]+recursion(str1[1:],str2[1:]) else: return "0"*len(str1) else: return "" return int(recursion(bin_left,bin_right),2)
@anthonya880
@anthonya880 9 ай бұрын
Please create playlist for Grind series of problems.
@2EOGIY
@2EOGIY 9 ай бұрын
Please let me know if I'm wrong, but the result will be the most significant bit or 0; its complexity could be reduced to O(1) just by doing AND on the most significant bit of a bigger number and filling the rest with zeroes. Am I right?
@sophiophile
@sophiophile 9 ай бұрын
There is the edge case where both numbers in the range are the same.
@2EOGIY
@2EOGIY 9 ай бұрын
@@sophiophile good point, but adding selection does not change complexity O(1), you could say O(2) as there will be only two cases checked.
@sophiophile
@sophiophile 9 ай бұрын
@@2EOGIY In Big O notation, you never say O(2), no matter the number of operations, as long as the number doesn't scale with input size, you say O(1) or 'constant time'. I get the point you are conveying of course, and you might be aware of what I said. But I thought I'd mention in just in case, because saying something like O(2) in a code interview would be seen as a red flag.
@2EOGIY
@2EOGIY 9 ай бұрын
@@sophiophile Thank you for pointing it out. Otherwise, I would died stupid. After research, I found: Big O - Upper Bound, Big Omega (Ω) - Lower Bound, Big Theta (Θ) - Tight Bound. So complexity can be described here as O(2), Ω(1), Θ(1)
@meetverma4812
@meetverma4812 9 ай бұрын
the second case is much much easier and logical
@coleyab
@coleyab 9 ай бұрын
You are the goat
@rydercasazza9745
@rydercasazza9745 9 ай бұрын
Cant this be done in constant time by XORing left and right to get the differing bits and just cutting them out?
@spsc07
@spsc07 9 ай бұрын
uhm just a random observation all the outputs will be in the form of 2^x, idk how to properly write it
@NeetCodeIO
@NeetCodeIO 9 ай бұрын
I think the result will be a summation of 2^x, but not necessarily 2^x
@bleh-hc9mw
@bleh-hc9mw 9 ай бұрын
Can you explain this more? I am not understanding why the solution isn't just 2^(right-left) @@NeetCodeIO
@bleh-hc9mw
@bleh-hc9mw 9 ай бұрын
If its less than 32 --- or else its 0 ?
@spsc07
@spsc07 9 ай бұрын
yeah summation of 2^x, thats the pattern@@NeetCodeIO
@nhatphung497
@nhatphung497 9 ай бұрын
Can you explain the %?
@Shujaathullakhan
@Shujaathullakhan 9 ай бұрын
well last one was intuitive
@CS_n00b
@CS_n00b 9 ай бұрын
maybe would have been better to start the video with the first approach since easier to understand
@DeathSugar
@DeathSugar 9 ай бұрын
Lol, my initial O(n) solution looked for biggest power of 2 within range and iterate from there
@pastori2672
@pastori2672 9 ай бұрын
wow
@user-fw4kz3bb4g
@user-fw4kz3bb4g 9 ай бұрын
Disliked the video after you explained the first solution. Then liked after seeing the last solution😂
@deathbombs
@deathbombs 9 ай бұрын
The prefix solution feels more intuitive to my monkey brain. We just look for where the leftmost binaries become equal
@tusharkumarraj6066
@tusharkumarraj6066 2 ай бұрын
couldnt understand much
@deathbombs
@deathbombs 9 ай бұрын
I hate bitwise
@rick-kv1gl
@rick-kv1gl 9 ай бұрын
jesus christ.
@MO-6352
@MO-6352 9 ай бұрын
My brain to me: GET A LIFE
@themusicalmonk2732
@themusicalmonk2732 Ай бұрын
wow
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 147 М.
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,9 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 51 МЛН
Minimum Height Trees - Leetcode 310 - Python
23:30
NeetCodeIO
Рет қаралды 21 М.
Rotating the Box - Leetcode 1861 - Python
15:14
NeetCodeIO
Рет қаралды 6 М.
RHCSA Exam Preparation | Dumps Solved & Step-by-Step Guide
2:50:23
Cloud Technology
Рет қаралды 1,2 М.
K Inverse Pairs Array - Leetcode 629 - Python
29:44
NeetCodeIO
Рет қаралды 16 М.
Power of Two - Leetcode 231 - Python
12:42
NeetCodeIO
Рет қаралды 16 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 642 М.
Partition Array for Maximum Sum - Leetcode 1043 - Python
27:33
NeetCodeIO
Рет қаралды 18 М.
Furthest Building You Can Reach - Leetcode 1642 - Python
11:08
NeetCodeIO
Рет қаралды 16 М.
Maximum Profit in Job Scheduling - Leetcode 1235 - Python
14:45
NeetCodeIO
Рет қаралды 34 М.
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,9 МЛН