Greatest Common Divisor Traversal - Leetcode 2709 - Python

  Рет қаралды 10,830

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/problems/greates...
0:00 - Read the problem
0:24 - Drawing Explanation
15:22 - Coding Explanation
leetcode 2709
#neetcode #leetcode #python

Пікірлер: 54
@akialter
@akialter 4 ай бұрын
Streak destroyer problem
@sayandey1478
@sayandey1478 3 ай бұрын
self destruction problem, every moment there is a twist
@edwardj4454
@edwardj4454 4 ай бұрын
Your explanations are always super well structured that after hearing the first few minutes I already get the intuition for solving the entire problem. Thanks!
@swanv951
@swanv951 4 ай бұрын
The n^2 solution too works very efficiently if you exit out when a number isn't "matched" with any other number
@siddharth-gandhi
@siddharth-gandhi 4 ай бұрын
tysm, figured out union find but was getting tle. prime explanation helped :D
@jamesabasifreke
@jamesabasifreke 4 ай бұрын
7:30 I doubt you can do union find merge in constant time. It’s usually log n time even with ranking and path compression 😊
@saminhasan87
@saminhasan87 4 ай бұрын
every video of yours deserves more than one like. Thank you for your efforts!
@MP-ny3ep
@MP-ny3ep 4 ай бұрын
Great explanation! Thank you so very much
@mnaresh3382
@mnaresh3382 4 ай бұрын
I think the part where finding the prime numbers could have been explained better, after some research this is what I found, The method that is used in this problem to achieve root(n) time complexity is called Trial Division Method, which follows three steps 1) Let us consider N be the given number for which we need to find the factors, so we initialize it 2) Iterate through all potential factors(p) starting from 2 till root(N), -For each p, check if p is a prime factor(divides N completely), if so add it to our factors, and then divide N by p ( N= N/p) -repeat the above step until it is no longer divisible by p, For me this is was hard to understand, but basically consider this, let us say we have no 60, ofcourse we know 64 is divisible by 2,4,8,16 but we don't need all those numbers except just 2, this can be achieved by repeatedly multiplying the N by 2(which is p) by doing this we are essentially reducing the Number N to the point where N will not be divisible by other numbers such as 4,8,16 etc. 3) after iterating until root(n) we would either get 1, or some other number, in that case, this particular number is the only prime-factor that we are left out with the set of all prime factors of N. I hope this helps, you can also check this simple python code which makes much more sense from taking a glance at it. def prime_factors(n): factors = [] # Check for divisibility by 2 separately while n % 2 == 0: factors.append(2) n = n // 2 # Check for divisibility by odd numbers from 3 up to sqrt(n) for i in range(3, int(n**0.5) + 1, 2): while n % i == 0: factors.append(i) n = n // i # If n is a prime greater than 2, add it to factors if n > 2: factors.append(n) return factors # Example usage: n = 84 print(prime_factors(n)) # Output: [2, 2, 3, 7]
@user-ml4je5pq7q
@user-ml4je5pq7q 4 ай бұрын
Please teach a ~30 min crash course on object oriented programming in python, like your other video "python for coding interviews". You are definitely the best teacher out there.
@saminhasan87
@saminhasan87 4 ай бұрын
where to find your advanced algorithm course for learning union find?
@Ari-pq4db
@Ari-pq4db 4 ай бұрын
Keep em coming ❤
@luisdmoralesh
@luisdmoralesh 4 ай бұрын
rlly good explanation, ty
@tizekodnoon8305
@tizekodnoon8305 4 ай бұрын
Neato!!! Brain's hurting!🤯
@mrmcpherson2722
@mrmcpherson2722 4 ай бұрын
This man has probably saved so many daily streaks at this point. 😂
@mudamalajayasimhareddy9517
@mudamalajayasimhareddy9517 4 ай бұрын
Thanks.🙃
@akhilchauhan9417
@akhilchauhan9417 4 ай бұрын
Using Sieve of Eratosthenes for building primes factors of each number. My question is as you can see I'm checking for factor in map twice, How can I modify it such that I only have to do it once other that writing a function for that? def canTraverseAllPairs(self, nums: List[int]) -> bool: m = len(nums) uf = UnionFind(m) mx = max(nums) factor = {} # f -> index of the number with factor f primes = [i for i in range(mx + 1)] for i in range(2, mx + 1): if primes[i] == i: j = i * i while j 1: while primes[n] != n: if primes[n] in factor: uf.union(i, factor[primes[n]]) else: factor[primes[n]] = i n = n // primes[n] if primes[n] in factor: uf.union(i, factor[primes[n]]) else: factor[primes[n]] = i return uf.count == 1
@varunpalsingh3822
@varunpalsingh3822 4 ай бұрын
tysm
@amaanshaikh7292
@amaanshaikh7292 4 ай бұрын
Hey, Can you also upsolve the weekly/biweekly contests it helps a lot Thankyou. Love your efforts ❤
@drewstake3881
@drewstake3881 4 ай бұрын
He should livestream the weekly contest, I think that'll be so entertaining see the thought process live
@michaelharris1305
@michaelharris1305 4 ай бұрын
yes please
@belphegor32
@belphegor32 4 ай бұрын
@ake3881 I think you have never attended a leetcode contest, because even if you think about it, its obvious, that it breaks the whole point of contest, there will be people who would just copy his solutions (it is prohibited by the rules to upload solutions before the end of contest, they even hide testcases, so people wont brute code some test cases). If you meant like uploading his thoughts and solutions right after contests, that is ok, but there are many people who already do this.
@mrf92
@mrf92 4 ай бұрын
1 is not a prime. because by definition a prime number can't be factorized with other prime numbers. but if you consider 1 as a prime then existence of other prime numbers isn't possible.
@user-gb5id1nt8v
@user-gb5id1nt8v 4 ай бұрын
140 th like done:)) less go! ur great teacher
@BlunderArmour
@BlunderArmour 4 ай бұрын
RIP streak Jan 2024 - Feb 2024.
@user-j5ja95
@user-j5ja95 4 ай бұрын
Do you do LC everyday while having a full time job? If so how do you do it? I'm so tired after work and don't wanna do anything, so I usually LC only on weekends
@ShikaIE
@ShikaIE 4 ай бұрын
I just solve daily challenge everyday. Should only take half hour at most except the problem like this 😂! Not really for interview prep only but just to keep my mind active because you rarely solve difficult algo problem at work.
@1vader
@1vader 4 ай бұрын
Afaik KZbin and working on the NeetCode website is his full time job.
@aidanthompson5053
@aidanthompson5053 4 ай бұрын
This is all a test of morality. Will you do the right thing: sacrifice your ego or betray your own people
@Kaviarasu_NS
@Kaviarasu_NS 4 ай бұрын
What tools do you use for making these tutorials?
@belphegor32
@belphegor32 4 ай бұрын
he said, that he just uses paint, I guess he has some personal settings in it. Some people use photoshop, which I think is also better to use, if you know how to set it up properly.
@Kaviarasu_NS
@Kaviarasu_NS 4 ай бұрын
@@belphegor32 I’m planning to create educational animation using adobe animate, was asking for a personal suggestion
@RuslanKovtun
@RuslanKovtun 4 ай бұрын
14:04 - Rounding is bad idea, better to rewrite it other way around - compare squares instead of square roots.
@thebigpaff
@thebigpaff 4 ай бұрын
that's what he did in the code
@rajsuriyang3427
@rajsuriyang3427 4 ай бұрын
I don't even know what is union find.
@Ari-pq4db
@Ari-pq4db 4 ай бұрын
Noice ❤
@get_out_it
@get_out_it 4 ай бұрын
too hard bro
@micorlov4321
@micorlov4321 4 ай бұрын
4:40 Is 1 a prime factor?
@IK-xk7ex
@IK-xk7ex 4 ай бұрын
I see that I'm still weak after solving 467 problems that I couldn't recognise and understand the problem by myself :(
@hiteshwadhwani629
@hiteshwadhwani629 4 ай бұрын
you are not alone 🥲
@lian1238
@lian1238 4 ай бұрын
Learn data structures. Learn algorithms. There are more than one way to solve any problem. If you know common algorithms, you can solve most leetcode problems because thats the answer they are looking for. Also remember that being bad at leetcode doesn’t mean you are bad at coding. Also, idk if itll help you, but the way i do leetcode is copy the code into my own editor and run a loop in the terminal that runs the file forever. So when i save, the new code and output are shown right away. Also use a lot of print statements. I then copy the cases from leetcode and run the function with their input and expected value.
@hiteshwadhwani629
@hiteshwadhwani629 4 ай бұрын
@@lian1238 thanks
@BurhanAijaz
@BurhanAijaz 4 ай бұрын
easier : class Solution: def canTraverseAllPairs(self, nums: List[int]) -> bool: if len(nums)==1:return True nums = set(nums) if 1 in nums:return False if len(nums)==1:return True nums = sorted(nums,reverse=True) for i in range(len(nums)-1): for j in range(i+1,len(nums)): if gcd(nums[i],nums[j])-1: nums[j]*=nums[i] break else: return False return True
@xtremeff5428
@xtremeff5428 4 ай бұрын
Thanks for this, I was trying hard but this helped me save my streak
@groot3271
@groot3271 4 ай бұрын
Start coding in C++ again...
@arijaa.9315
@arijaa.9315 4 ай бұрын
To be honest it is hard for me .
@aidanthompson5053
@aidanthompson5053 4 ай бұрын
You are obligated to get rich so you can give back to your people and free them from financial slavery
@cocoscacao6102
@cocoscacao6102 4 ай бұрын
I bet you'd like my algo problem... Wanna solve it for me xD?
@rohan14040
@rohan14040 4 ай бұрын
i hate this problem
@ethronixNativeCX
@ethronixNativeCX 4 ай бұрын
TAGGR The first FULLY decentralized social network powered by the Internet Computer.
Subarrays with K Different Integers - Leetcode 992 - Python
17:31
K Inverse Pairs Array - Leetcode 629 - Python
29:44
NeetCodeIO
Рет қаралды 15 М.
DAD LEFT HIS OLD SOCKS ON THE COUCH…😱😂
00:24
JULI_PROETO
Рет қаралды 14 МЛН
One moment can change your life ✨🔄
00:32
A4
Рет қаралды 32 МЛН
Maximum Value of K Coins from Piles - Leetcode 2218 - Python
18:04
Bitwise AND of Numbers Range - Leetcode 201 - Python
18:25
NeetCodeIO
Рет қаралды 12 М.
Arithmetic Slices II - Leetcode 446 - Python
21:19
NeetCodeIO
Рет қаралды 16 М.
Cherry Pickup II - Leetcode 1463 - Python
24:00
NeetCodeIO
Рет қаралды 15 М.
The ORDER BY Algorithm Is Harder Than You Think
13:46
Tony Saro
Рет қаралды 58 М.
Basic System Design for Uber or Lyft | System Design Interview Prep
16:18
DAD LEFT HIS OLD SOCKS ON THE COUCH…😱😂
00:24
JULI_PROETO
Рет қаралды 14 МЛН