High Definition Standard Definition Theater
Video id : 2KqZX8fG_wY
ImmersiveAmbientModecolor: #c9cabe (color 2)
Video Format : (720p) openh264 ( https://github.com/cisco/openh264) mp4a.40.2 | 44100Hz
Audio Format: 140 ( High )
PokeEncryptID: f81b554f18205afe142673bcd04adbc281315486e4f9ca52c0de466584925ab7d8bd66ed6db9ef9ba1fb64aaa7552dbb
Proxy : cal1.iv.ggtyler.dev - refresh the page to change the proxy location
Date : 1732405967161 - unknown on Apple WebKit
Mystery text : MktxWlg4Zkdfd1kgaSAgbG92ICB1IGNhbDEuaXYuZ2d0eWxlci5kZXY=
143 : true
Leetcode 1512 - Number of Good Pairs
 60 FPS video
34,071 Views • Oct 30, 2024 • Click to toggle off description
Leetcode 1512 - Number of Good Pairs
Metadata And Engagement

Views : 34,071
Genre: Education
Uploaded At Oct 30, 2024 ^^


warning: returnyoutubedislikes may not be accurate, this is just an estiment ehe :3
Rating : 4.929 (38/2,104 LTDR)

98.23% of the users lieked the video!!
1.77% of the users dislieked the video!!
User score: 97.34- Overwhelmingly Positive

RYD date created : 2024-11-23T21:26:02.40555Z
See in json
Tags
Connections
Nyo connections found on the description ;_; report an issue lol

25 Comments

Top Comments of this video!! :3

@GregHogg

3 weeks ago

Master data structures and algorithms for FREE at algomap.io/ :)

|

@kubs1162

3 weeks ago

Remember, with enough practice you can fit a hashmap anywhere kids

39 |

@vishalparth1766

3 weeks ago

Answer should be 4 but by your way it will be 3, actually we need to calculate n choose 2 or n(n-1)/2, where n is the number of 1's or let say m's in the given array where you are just subtracting 1 from each n.

So the edit would be, inside of the for loop remove everything and add the following

k = counter[num]
good_pairs += (k*(k-1))//2

This would give the correct result.

Love your content by the way ❤️

67 |

@MADorMad

2 weeks ago

it is spread to every number to C(n,k) and sum it.

3 |

@миш-т5й

3 weeks ago

you should not subtract 1, you should check number of compositions😊

1 |

@Mr-Raptor

2 weeks ago

In javascript create an object and do nums.foreach to add stuff to it o[num] = (o[num] || 0) + 1
and then return Object.values(o).reduce acc + num--*num/2 with initial 0

|

@davidgillies620

3 weeks ago

For toy examples there's not much need to optimise, but big arrays will be able to use the vectorisation built into pandas and numpy.

In fact, it's basically a one-liner in numpy:
def goodpairs(arr: list) -> int:
return np.sum(np.vectorize(npairs)(np.bincount(np.array(arr))))

with npairs as:
def npairs(n: int) -> int:
return (n * n - n) // 2

1 |

@adityaanuragi6916

3 weeks ago

Explanation of the code and theory behind it

If a number occurs 4 times then good pairs are - 6 (3+2+1)

If a number occurrences 6 times its (5 + 4.... +1)

So we can make a counter to get the frequencies and to calculate 5+4+3+2+1 when occurrences = 6 (we use the formula sum of an arithmetic progression
as the required n + (n-d) + (n-2d) is an arithmetic progression)
(n * (2a + (n-1)*d))/2

Where n = total elements in the series (n = 5 when we have 5+4+3+2+1)

a = starting element (in our case 5)

d = difference between 2 elements (in our case -1 because 4-5 = -1 and also 3-4 = -1)

Then we use this formula and apply it to all occurrences in the counter hashmap and return the sum

9 |

@tRuStThEsCiEnCeBiGoT

4 days ago

Nice how he ends early to skip the ending of his explanation, where he realizes that he's WRONG, and rather than NOT POST A WRONG VIDEO, he just chops off the ending. If you follow out his logic, subtracting 1 from each entry and adding them up, then you get an answer of 3, which is clearly wrong.

|

@alxjones

3 weeks ago

Get all the counts, do n choose 2 on each, sum them up. n choose 2 = n(n-1)/2, so we can factor out the 2 and do that at the end.

```python
def numIdenticalPairs(self, nums: List[int]) -> int:
return sum(
count * (count - 1)
for count in Counter(nums).values()
) // 2
```

3 |

@xamoxer1

3 weeks ago

It doesn't make sense to make these videos with errors when you have everything on leetcode. Waste of time

1 |

@bouhnibabderrahmane306

3 weeks ago

let me know what do u think about my solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
D = {}
D_ = {}
count =0
for i in range(len(nums)):
if nums[i] not in D:
D[nums[i]] = i
D_[nums[i]] = 0
else:
if D[nums[i]] <i:
count += 1+D_[nums[i]]
D_[nums[i]] +=1
return count

|

Go To Top