Tuple with Same Product - Leetcode 1726 - Python

  Рет қаралды 7,986

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 47
@sharanvaitheeswaran
@sharanvaitheeswaran 22 сағат бұрын
Here is an intuitive way of looking at the problem: When we have a frequency of more than 2 for any product, the number of pairs then becomes the number of ways to choose two pairs out of the total number of pairs * 8. For example, if the product frequency count of 12 is 'x'. then the contribution of 12 to the answer is xC2*8 , i.e (x*(x-1))//2)*8.
@motherofsuper
@motherofsuper 20 сағат бұрын
Another way is to think about it as 4 * freq * (freq - 1) The 4 comes from 2! * 2! = 2 * 2 (2! is the number of ways to arrange 2 numbers, where the order matters. And we have 2 pairs here) Then freq * (freq - 1) is because we have "freq" ways to pick the first pair, and "freq - 1" ways to pick the second pair
@Anshul-qb2pm
@Anshul-qb2pm 18 сағат бұрын
combinations will be like this always 1)(ab,cd) 2)(ab,dc) 3)(ba,cd) 4(ba,dc) 5)(cd,ab) 6)(cd,ba) 7)(dc,ab) 8)(dc,ba)
@blurryfca3
@blurryfca3 9 сағат бұрын
​@@motherofsuper i made a little roundabout way and came to this in the end. Started with observing count of products of 2 numbers-> prod_freq = n. prod_freq = 1 gave 0 combinations, 2 gave 1, 3 gave 3 combinations; so came out to be n*(n-1)//2. then I noticed oh damn they are looking for permutations not combinations, so tweaked it account for permutations, 2! ways to choose a pair, 2!*2! ways to permute each pair and voila its 2!2!2!* n(n-1)//2
@h0td0g
@h0td0g 14 сағат бұрын
man i wished neetcode explain a little more about how that formula came about. all he needed to do was to reference C(X,2) in Combinations terms and things would click so much easier. 2 hash map approach is so unnatural, if you were to get this in an interview nobody would be draw this whole thing out to find a whole ass pattern.
@ArunKumarG-j5y
@ArunKumarG-j5y 17 сағат бұрын
i do have one confusion, for count 2 formula is 2 *(1//2) in python // is performs floor division right ? like for 0.5 floor value is 0 , then answer will be 2 * 0 for count 2 answer will be 0
@VikasKumarSingh-m4h
@VikasKumarSingh-m4h 14 сағат бұрын
Heres how i solved it : valid pair makes 8 tuples so iterating over each pair and storing there product in a hashmap then check if the map has the product if it has then we can simply multiply 8 to the no of occurrences of product in the map and add to the count to get the result
@manikishore9646
@manikishore9646 7 сағат бұрын
Easiest inutition: It is a problem of combinations and permutations. Select 2 pairs from available pairs and there will be 8 permutations for each selection.
@Avi_Bedi2125
@Avi_Bedi2125 11 сағат бұрын
hope it helps: NCR = N! / (R! * (N-R)!) -> number of combinations when we have N entities and we have to select R entities out of them. Therefore, NC2 = N! / (2 * (N-2)!) = N * (N-1) / 2 this is how we get the formula!
@yhbarve
@yhbarve 22 сағат бұрын
One of the more satisfying problems.
@_grind_mindset
@_grind_mindset 9 сағат бұрын
Selecting the number of pairs from a given count is just a method of combinatorics, where we need to select 2 pairs from the given number of count. The mathematical equation is pretty simple , nCr = n!/(r!*(n-r)!). Since the case is of choosing 2 pairs the above formula can be rewritten as - (n * (n-1)) / 2, where n is the number of pairs.
@user_unknown04
@user_unknown04 5 сағат бұрын
Its just a piece of combinatorics. Basically we are choosing no. of pairs C 2. for example the no. of ways of choosing 2 things out of 4 is 4C2 = 4 * 3 / 2 = 12 / 2 = 6 and thats pretty much of it
@vincentwong9007
@vincentwong9007 6 сағат бұрын
The intuition I used for understanding the maths: Pair i can be combined with the n-1 other pairs to create the tuples. Repeat for the other pairs to get a total of n*(n-1) combinations. However this doesn't account for the duplicate combinations where i,j and j,i are counted as the same combination so you use /2.
@firasyousfi2269
@firasyousfi2269 6 сағат бұрын
My understanding is if we check we take two pairs (a,b) and (c,d) with a product equal to X, then we have 8 possible tuples. (1) So first we create a hashmap that maps every product to how many pairs result in that product. Then we just take the point explained in (1) and we multiply by 8 the number of ways to take 2 pairs from a total of N which is C(N, 2) = N * (N - 1) / 2
@bigmanhalalreaper2290
@bigmanhalalreaper2290 9 сағат бұрын
what is ur paint software please
@SandyCr7
@SandyCr7 9 сағат бұрын
Sum of natrual numbers N * (N+1)/2 Just use N as N-1 (N-1) * (N-1+1) /2 (N-1) * N / 2
@deadlyecho
@deadlyecho 7 сағат бұрын
Count * (Count -1) * 4
@emilturbo5
@emilturbo5 9 сағат бұрын
One thing I couldn't understand much is: How come it requires more time and extra memory for the algorithm to return the answer of the given problem when I implement the product variable of the two integers at index i and j inside the two for-loop expressions before assigning the product as its key in the hash map, compared to directly assigning the product of the two integers at the hash map without implementing it as a separate variable? Wouldn't it make sense for the algorithm to then require less time and space for execution without needing to recompute the product value when directly assigning it to the declared hash map?
@bertmorris9976
@bertmorris9976 21 сағат бұрын
For me the intuition is the number of points is an n choose 2 problem which simplifies to (n^2 -n) / 2
@LeonLeonLeonardo
@LeonLeonLeonardo 9 сағат бұрын
I get the idea, but I feel like if I have to solve this problem next month, I'll need to watch this video again. :(
@I.II..III...IIIII.....
@I.II..III...IIIII..... 13 сағат бұрын
Gauss's sum there is really overcomplicated. You can simply do 8 * product_cnt if product_cnt is greater than 0, then product_cnt += 1. Because if you have found a new pair, you just place it in tuples with each of the previous ones (and each will give you 8 tuples). In this way you don't even need a second for loop, just the nested one.
@robertbaindourov134
@robertbaindourov134 15 сағат бұрын
Gauss figured out n(n+1)/2 for sum of a series. n(n-1)/2 is the binomial coefficient / combination formula, or triangular number.
@anandmohansingh2390
@anandmohansingh2390 19 сағат бұрын
when nothing works, use hashmap
@youssefmamdouh4987
@youssefmamdouh4987 8 сағат бұрын
its simplified equation of n choose 2
@ajitpalsingh606
@ajitpalsingh606 18 сағат бұрын
magic is sum of n numbers is n*(n-1)/2
@punk5513
@punk5513 22 сағат бұрын
Bro's timing is🔥🔥🔥
@julianelmasry9556
@julianelmasry9556 22 сағат бұрын
hashmap jutsu is key when dealing with pairs
@anandailyasa2530
@anandailyasa2530 7 сағат бұрын
Hi, I wonder what tools (hardware & software) does neetcode use for Drawing Explanation? anyone please help
@testshubham
@testshubham 5 сағат бұрын
Instead using two maps can use num of n natural numbers.
@rangoliatti4890
@rangoliatti4890 7 сағат бұрын
how do i get the code in java
@MO-fg2cm
@MO-fg2cm 7 сағат бұрын
Use your intuition and implement
@deadlyecho
@deadlyecho 7 сағат бұрын
I love counting problems, discrete mathematics ❤
@AllThingsJS-ey6ww
@AllThingsJS-ey6ww 18 сағат бұрын
11th :)
@Zenetz-le4mq
@Zenetz-le4mq 9 сағат бұрын
He loves it we re doomed!
@lan10ak
@lan10ak 9 сағат бұрын
1726 -> 1 + 7 = 2 + 6 Just coincidence
@user_unknown04
@user_unknown04 5 сағат бұрын
cool pull my boy
@SandeepWithAI
@SandeepWithAI 20 сағат бұрын
does questions like these are asked in real interviews where we have to derive a mathematical formula to solve it omg!...
@motherofsuper
@motherofsuper 20 сағат бұрын
Uhh, isn't permutations & combinations one of the most basic principles in high school maths anywhere in the world?
@__amkhrjee__
@__amkhrjee__ 22 сағат бұрын
Respect 🫡
@ajitpalsingh606
@ajitpalsingh606 18 сағат бұрын
i was able to reach till count should>=2
@amanaryan7271
@amanaryan7271 3 сағат бұрын
nP2*4
@chih-jenwu4992
@chih-jenwu4992 20 сағат бұрын
maybe we can use pairs = math.comb(cnt, 2) instead of (cnt * cnt(-1)) // 2
@dekumidoriya-qi2is
@dekumidoriya-qi2is 8 сағат бұрын
i solved it this way and i think it helps in lot of such problems with two pairs we have 8 combinations as the number of pairs increases the number of combinations of two pairs become 1 + 2 + 3 + .... + n - 1 so its just sum of (n - 1) natural numbers * 8 :) ==> (n)*(n - 1) * 4 hope this helps
@maggiechen5430
@maggiechen5430 22 сағат бұрын
first
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 837 М.
Tuple with Same Product | Leetcode 1726
13:36
Techdose
Рет қаралды 5 М.
JISOO - ‘꽃(FLOWER)’ M/V
3:05
BLACKPINK
Рет қаралды 137 МЛН
Who is More Stupid? #tiktok #sigmagirl #funny
0:27
CRAZY GREAPA
Рет қаралды 10 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 209 М.
The Genius Way Computers Multiply Big Numbers
22:04
PurpleMind
Рет қаралды 332 М.
Jeff Dean: AI will Reshape Chip Design - NeurIPS 2024
43:53
GradientSpills
Рет қаралды 6 М.
1726. Tuple with Same Product | Hash Table | Math | 3 Approaches
19:39
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 304 М.
Understanding Ownership in Rust
25:30
Let's Get Rusty
Рет қаралды 279 М.
One second to compute the largest Fibonacci number I can
25:55
Sheafification of G
Рет қаралды 476 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН