FAANG Coding Interviews / Data Structures and Algorithms / Leetcode
Пікірлер: 87
@GregHoggАй бұрын
If you're from India, please watch til the end - you will probably be surprised haha
@tejakovvuri3042Ай бұрын
Surprised for real 😂
@user-qx4sk3ud6nАй бұрын
Haha surprised😂
@mangoman-vx4pzАй бұрын
The last part❤
@lakshyamathur8808Ай бұрын
Surprised indeed😂😂
@harshnj27 күн бұрын
that was really great!
@prathamyadav2422Ай бұрын
In last ,You sang it really well 😂👍 Love from India ❤🇮🇳
@user-qx4sk3ud6nАй бұрын
for i in range(0,♾️): print("Thank you. Love from India")
@yashwanthkumar2513Ай бұрын
TLE
@shreehari2589Ай бұрын
Memory be like : is it a free real estate for you?
@batteraquette5843Ай бұрын
just do "while True:"
@pavliv28 күн бұрын
You crashed by browser
@ARkhan-xw8ud10 күн бұрын
it will throw error
@ngneerinАй бұрын
That's not how Indians solve it. Here is how we do: def isArraySpecial(nums): for i in range(1, len(nums)) : if nums[i - 1]&1 != nums[i]&1: return False return True
@KonstantinUbАй бұрын
Using len() inside of range() is a code smell; you should use zip() instead :)
@not_vinkamiАй бұрын
@@KonstantinUb I don't see how zip can make this code simpler tho
@KonstantinUbАй бұрын
@@not_vinkami return all( x & 1 != y & 1 for x, y in zip(nums, nums[1:]) )
@_greysama_Ай бұрын
Welldone. But as a computer science major, we use if ((nums[i] ^ nums[i-1]) & 1): return False return True Ps: Doing this just for fun, there is much better way to do it by reducing calculations..like just taking the parity of first element and comparing it with others..that way is faster
@KonstantinUbАй бұрын
@@_greysama_ As a computer science major, you should be aware that `x & 1 != y & 1` and `(x ^ y) & 1` are completely equivalent as boolean expressions. In fact, in a compiled language like C/C++, they produce the exact same machine instructions (check with Godbolt). In Python, they generate equivalent bytecode (check with `import dis`). So there is no difference, other than the first being a tad more readable / communicating the intent a bit more clearly, in my opinion. Again, using indices like that *in Python specifically* is considered non-Pythonic and a code smell. A pairwise iterator is recommended instead.
@davidgillies620Ай бұрын
You can take the bitwise XOR of adjacent elements and look at the LSB. def pairwiseparity(nums: list) -> bool: pparity = True for i in range(1, len(nums)): pparity = pparity and bool((nums[i] ^ nums[i - 1]) % 2) return pparity
@DavideCanton25 күн бұрын
True , but you can avoid traversing the entire list if pparity becomes false
@JohnDoe-sp3dcАй бұрын
Why would you use string comparison for the parity check instead of a Boolean?
@joaooliveira9852Ай бұрын
I suppose it was just to be more intuitive for the viewer
@michaelvankley862Ай бұрын
Because why not? It’s readable and won’t make a performance difference. To be fair, I probably would have used a function called is_even() and returned a Boolean, but there’s a lot of ways to express the same reasoning.
@xingyuxiang1637Ай бұрын
One can also use pairwise from Python for more general cases.
@tommasochiti4237Ай бұрын
@@michaelvankley862Won’t make a performance difference? A string, on a modern PC is at least 8bytes (the pointer to the heap allocated region) and usually you also have a capacity and a size (which are two integers). A boolean is as simple as a bit.
@KahonekiАй бұрын
@@tommasochiti4237 i agree wholeheartedly with what you're saying. Minor correction though - a boolean is still stored as a byte, not a bit. This is because a byte is the smallest addressable size in memory :]
@nagasaijairam2343Ай бұрын
Love from India 🎉
@user-lz9ld5tj6i4 күн бұрын
it’s O(2n), instead you should store compiled parity in a memo for O(n)
@tranhung5493Ай бұрын
I think we can do this way: if (nums[i-1] + nums[i])%2==0 then False
@victormoisa6935Ай бұрын
You might run into some integer overflows with this one, I think it would still work just keep an eye on that. Also not a problem on Python
@asagiai4965Ай бұрын
That actually could work, just put some extra conditions.
@edudictivecoder4644Ай бұрын
Chak de India 😍😍😍😍 I follow your shorts to improve my last submitted codes on leet code if you post those problem shorts otherwise will take your shrot as motivation for my next problem on leetcode
@jbparkes19028 күн бұрын
You could also check whether the sum arr[i] + arr[i+1] is always odd. This is probably computationally cheaper.
@arsheyajain7055Ай бұрын
Very good 😊
@lakshyamathur8808Ай бұрын
Very well done Greg 🎉🎉
@hlubradio231825 күн бұрын
Damn Greg! You've done it again. I gotta find some time.
@kartekjadhav8481Ай бұрын
Love from India ❤
@abhi.4164Ай бұрын
Thanks buddy
@AbhishekRai-qp7wwАй бұрын
Loved it Greg❤
@crissdellАй бұрын
Love from Indiaaa
@amarjeetusnale4699Ай бұрын
love from INDIA
@indyplaygames3066Ай бұрын
for in range(1, len(nums)): if nums[i] % 2 == nums[i - 1] % 2: return False return True
@augustinradjou3909Ай бұрын
After 2 months of break I am again starting with leetcode..thanks man...this video Kickstart me
@parthrathod2607Ай бұрын
This was an array now think in terms of multi dimensional Matrix 😅
@youthpost_34Ай бұрын
Got the logic of algo ❤
@denysdanov88Ай бұрын
Parity function is overkill and makes it slower, just use if nums[i] % 2 == nums[i - 1] % 2 And avoid string comparison at any cost especially in this question
@nexushare8105Ай бұрын
why?
@KonstantinUbАй бұрын
The performance impact is negligible, and the function makes the code a bit more readable. But string comparisons are definitely unnecessary, agreed on that one.
@pulkitjain813528 күн бұрын
Didn't expect last part, chak de india
@user-uj2mt6nv2gАй бұрын
You can speed this up to be twice as fast on average with a simple check. If p(num[0]) = p(num[last odd index]) return false. For a random data there is a 50% chance that the last element is of the wrong parity and since you can do this before the cycle you dont add any complexity.
@not_vinkamiАй бұрын
We can simplify the condition to just num[i] ^ num[i+1] & 1 == 0 because the xor operation can be interpreted as returning 1 if the inputs are different. I'm not sure if this is faster tho
@kotXbitАй бұрын
Why not use a bitwise op for the parity check
@danieledispirito2692Ай бұрын
the best (elegant) solution is the bitwise one: if (first & 1) ^ (second & 1) == 0 return False
@user-uj2mt6nv2gАй бұрын
Another thing - you dont have to compare elements at all. Just use (if nums[i] % 2 == A) where A is redefined as A = A XOR 1). This will work like 10 times faster compared to authors solution
@naivedyam2675Ай бұрын
A better solution would be adding the two numbers. Odd+Odd gives even and even+even also gives even. So we just have to check if the sum is even or not and if it is then we return false
You're calculating the parity of each number twice (except for the edges)
@DestimateАй бұрын
W song
@shreehari2589Ай бұрын
I believe 30% to 40% of your audience are from India 😂
@nigh_anxiety29 күн бұрын
This solution is checking every index twice. Instead, you can set a boolean flag based on whether index 0 is odd or even. Then make sure the flag flips at each index. Or you could use bitwise XOR for every pair of indexes and just verify the XOR result is true.
@sanscipher9166Ай бұрын
for i in range is an unpythonic code smell.
@dakshbhatnagarАй бұрын
NPCI asked this? 😳
@f.herumusu8341Ай бұрын
Some people consider multiple returns as bad style and each elements parity is calculated two times. If you have a picky interviewer ...
@mytube3486Ай бұрын
😂😂
@joyxprogamers200529 күн бұрын
So Indian companies do ask leetcode pdoblems 😂😂😂
@cstates9427 күн бұрын
This is taking 'readability over performance' waaaay too far. At least put a temporary bool to cut calculations in half.
@SehyoАй бұрын
Man is doing excessive string comparisons
@creativeusername4400Күн бұрын
Lol
@itsmaxim01Ай бұрын
so you do useless string comparison operations (“even” and “odd”, O(n+1) complexity) instead of comparing booleans (O(1)). sounds like something the average indian uber eats driver would do.
@technicallytechnical1Ай бұрын
lil bro stop hating
@sachinpangal219426 күн бұрын
Bro give me a leetcode premium 😅
@GregHogg26 күн бұрын
Noooo
@sachinpangal219426 күн бұрын
@@GregHogg bro replied legit to defend himself 🥲😭
@GregHogg26 күн бұрын
@@sachinpangal2194 I genuinely have no idea what you're talking about
@sachinpangal219426 күн бұрын
@@GregHogg nothing bro u just motivate me to code 😤
@KonstantinUbАй бұрын
Using indices to iterate pairwise through a list is not Pythonic. Try this instead: def parity(x): return x % 2 == 1 return all( parity(x) != parity(y) for x, y in itertools.pairwise(nums) ) And if you want to do it using only builtins, you can implement `pairwise` yourself: def pairwise(nums): return zip(nums, nums[1:])
@sridharaniavutu1112Ай бұрын
Hey Gregg I messaged you in LinkedIn U didn't reply yet