India - This one is for you! Special Array, Leetcode 3151

  Рет қаралды 53,750

Greg Hogg

Greg Hogg

Ай бұрын

FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

Пікірлер: 87
@GregHogg
@GregHogg Ай бұрын
If you're from India, please watch til the end - you will probably be surprised haha
@tejakovvuri3042
@tejakovvuri3042 Ай бұрын
Surprised for real 😂
@user-qx4sk3ud6n
@user-qx4sk3ud6n Ай бұрын
Haha surprised😂
@mangoman-vx4pz
@mangoman-vx4pz Ай бұрын
The last part❤
@lakshyamathur8808
@lakshyamathur8808 Ай бұрын
Surprised indeed😂😂
@harshnj
@harshnj 27 күн бұрын
that was really great!
@prathamyadav2422
@prathamyadav2422 Ай бұрын
In last ,You sang it really well 😂👍 Love from India ❤🇮🇳
@user-qx4sk3ud6n
@user-qx4sk3ud6n Ай бұрын
for i in range(0,♾️): print("Thank you. Love from India")
@yashwanthkumar2513
@yashwanthkumar2513 Ай бұрын
TLE
@shreehari2589
@shreehari2589 Ай бұрын
Memory be like : is it a free real estate for you?
@batteraquette5843
@batteraquette5843 Ай бұрын
just do "while True:"
@pavliv
@pavliv 28 күн бұрын
You crashed by browser
@ARkhan-xw8ud
@ARkhan-xw8ud 10 күн бұрын
it will throw error
@ngneerin
@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
@KonstantinUb Ай бұрын
Using len() inside of range() is a code smell; you should use zip() instead :)
@not_vinkami
@not_vinkami Ай бұрын
​@@KonstantinUb I don't see how zip can make this code simpler tho
@KonstantinUb
@KonstantinUb Ай бұрын
@@not_vinkami return all( x & 1 != y & 1 for x, y in zip(nums, nums[1:]) )
@_greysama_
@_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
@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
@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
@DavideCanton
@DavideCanton 25 күн бұрын
True , but you can avoid traversing the entire list if pparity becomes false
@JohnDoe-sp3dc
@JohnDoe-sp3dc Ай бұрын
Why would you use string comparison for the parity check instead of a Boolean?
@joaooliveira9852
@joaooliveira9852 Ай бұрын
I suppose it was just to be more intuitive for the viewer
@michaelvankley862
@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
@xingyuxiang1637 Ай бұрын
One can also use pairwise from Python for more general cases.
@tommasochiti4237
@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
@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
@nagasaijairam2343 Ай бұрын
Love from India 🎉
@user-lz9ld5tj6i
@user-lz9ld5tj6i 4 күн бұрын
it’s O(2n), instead you should store compiled parity in a memo for O(n)
@tranhung5493
@tranhung5493 Ай бұрын
I think we can do this way: if (nums[i-1] + nums[i])%2==0 then False
@victormoisa6935
@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
@asagiai4965 Ай бұрын
That actually could work, just put some extra conditions.
@edudictivecoder4644
@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
@jbparkes190
@jbparkes190 28 күн бұрын
You could also check whether the sum arr[i] + arr[i+1] is always odd. This is probably computationally cheaper.
@arsheyajain7055
@arsheyajain7055 Ай бұрын
Very good 😊
@lakshyamathur8808
@lakshyamathur8808 Ай бұрын
Very well done Greg 🎉🎉
@hlubradio2318
@hlubradio2318 25 күн бұрын
Damn Greg! You've done it again. I gotta find some time.
@kartekjadhav8481
@kartekjadhav8481 Ай бұрын
Love from India ❤
@abhi.4164
@abhi.4164 Ай бұрын
Thanks buddy
@AbhishekRai-qp7ww
@AbhishekRai-qp7ww Ай бұрын
Loved it Greg❤
@crissdell
@crissdell Ай бұрын
Love from Indiaaa
@amarjeetusnale4699
@amarjeetusnale4699 Ай бұрын
love from INDIA
@indyplaygames3066
@indyplaygames3066 Ай бұрын
for in range(1, len(nums)): if nums[i] % 2 == nums[i - 1] % 2: return False return True
@augustinradjou3909
@augustinradjou3909 Ай бұрын
After 2 months of break I am again starting with leetcode..thanks man...this video Kickstart me
@parthrathod2607
@parthrathod2607 Ай бұрын
This was an array now think in terms of multi dimensional Matrix 😅
@youthpost_34
@youthpost_34 Ай бұрын
Got the logic of algo ❤
@denysdanov88
@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
@nexushare8105 Ай бұрын
why?
@KonstantinUb
@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.
@pulkitjain8135
@pulkitjain8135 28 күн бұрын
Didn't expect last part, chak de india
@user-uj2mt6nv2g
@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
@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
@kotXbit Ай бұрын
Why not use a bitwise op for the parity check
@danieledispirito2692
@danieledispirito2692 Ай бұрын
the best (elegant) solution is the bitwise one: if (first & 1) ^ (second & 1) == 0 return False
@user-uj2mt6nv2g
@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
@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
@user-oj9iz4vb4q
@user-oj9iz4vb4q Ай бұрын
set(map(lambda (i,x): i%2 == x%2, enumerate(array))).size()==1
@KatariaDeepak
@KatariaDeepak Ай бұрын
🇮🇳🇮🇳🇮🇳
@albin_joby
@albin_joby Ай бұрын
love from India
@blackHeretic00
@blackHeretic00 Ай бұрын
haha didn't expect this from you
@lakku153
@lakku153 Ай бұрын
Very 👍
@TheCrazyCoder-tb3ux
@TheCrazyCoder-tb3ux Ай бұрын
What coding language do you use ?
@laytonjr6601
@laytonjr6601 Ай бұрын
You're calculating the parity of each number twice (except for the edges)
@Destimate
@Destimate Ай бұрын
W song
@shreehari2589
@shreehari2589 Ай бұрын
I believe 30% to 40% of your audience are from India 😂
@nigh_anxiety
@nigh_anxiety 29 күн бұрын
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
@sanscipher9166 Ай бұрын
for i in range is an unpythonic code smell.
@dakshbhatnagar
@dakshbhatnagar Ай бұрын
NPCI asked this? 😳
@f.herumusu8341
@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
@mytube3486 Ай бұрын
😂😂
@joyxprogamers2005
@joyxprogamers2005 29 күн бұрын
So Indian companies do ask leetcode pdoblems 😂😂😂
@cstates94
@cstates94 27 күн бұрын
This is taking 'readability over performance' waaaay too far. At least put a temporary bool to cut calculations in half.
@Sehyo
@Sehyo Ай бұрын
Man is doing excessive string comparisons
@creativeusername4400
@creativeusername4400 Күн бұрын
Lol
@itsmaxim01
@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
@technicallytechnical1 Ай бұрын
lil bro stop hating
@sachinpangal2194
@sachinpangal2194 26 күн бұрын
Bro give me a leetcode premium 😅
@GregHogg
@GregHogg 26 күн бұрын
Noooo
@sachinpangal2194
@sachinpangal2194 26 күн бұрын
@@GregHogg bro replied legit to defend himself 🥲😭
@GregHogg
@GregHogg 26 күн бұрын
@@sachinpangal2194 I genuinely have no idea what you're talking about
@sachinpangal2194
@sachinpangal2194 26 күн бұрын
​@@GregHogg​ nothing bro u just motivate me to code 😤
@KonstantinUb
@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
@sridharaniavutu1112 Ай бұрын
Hey Gregg I messaged you in LinkedIn U didn't reply yet
@harshmpatil
@harshmpatil Ай бұрын
Love from India ❤
What is Constant Time / O(1) Complexity in DSA?
0:54
Greg Hogg
Рет қаралды 36 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 315 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 81 МЛН
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 112 МЛН
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 615 М.
Coin Change - Leetcode 322 - Dynamic Programming (Python)
15:27
Greg Hogg
Рет қаралды 2,3 М.
Kth Largest Element in an Array - Leetcode 215 - Heaps (Python)
11:52
Leetcode 46. Permutations : Introduction to backtracking
10:06
ComputerBread
Рет қаралды 88 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 774 М.
How I started coding from 0 and cracked Amazon, Google & Microsoft
9:43
Ashish Pratap Singh
Рет қаралды 458 М.
How I make HARD coding problems look EASY
8:04
Sahil & Sarra
Рет қаралды 100 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 232 М.
Stacks & Queues - DSA Course in Python Lecture 5
14:58
Greg Hogg
Рет қаралды 877