PokeVideoPlayer v23.9-app.js-020924_
0143ab93_videojs8_1563605 licensed under gpl3-or-later
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
Top Comments of this video!! :3
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 |
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 |
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 |
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.
|
@GregHogg
3 weeks ago
Master data structures and algorithms for FREE at algomap.io/ :)
|