Number of Good Pairs - Leetcode 1512 - Python

  Рет қаралды 17,090

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 20
@kirillzlobin7135
@kirillzlobin7135 Жыл бұрын
This man should be the first one who solved all leetcode problems on KZbin
@nur_6ek
@nur_6ek 9 ай бұрын
bro you are legend 😅
@priyam86f
@priyam86f Жыл бұрын
A fun story. When I first looked at the problem, I thought of searching for a pattern, let's say. if the array had [1,1] number of pairs = 1. similarly, if nums = [1,1,1], pairs = 3. now, if nums=[1,1,1,1] pairs = 6. finally, if nums = [1,1,1,1,1] pairs = 10. so by now I was sure, the "number of pairs" had a pattern while increasing, at first it was 1,then 3 then 6 and then 10. which means, it first incremented by just 2 [3], then by 3 [6] , then by 4[10]. I just couldn't code the solution up,but i guess i was on the same page! Intuitions are just crazy,cheers neet.
@picnicbros
@picnicbros Жыл бұрын
The pair-counting is a sub problem of LeetCode 2421- Number of Good Paths
@sadiksikder7014
@sadiksikder7014 Жыл бұрын
It's actually the combination formula nCr
@NikolayMishin
@NikolayMishin Жыл бұрын
very good explanation, thank you
@divyeshbatra6719
@divyeshbatra6719 8 ай бұрын
class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: resultdict={} sumresult=0 for index, x in enumerate(nums): if x in resultdict: resultdict[x].append(index) else: resultdict[x] = [index] for key,value in resultdict.items(): if len(value)>1: sumresult = sumresult+sum(range(len(value))) return sumresult
@JammUtkarsh
@JammUtkarsh Жыл бұрын
Greats It's my Day 29 streak question!
@two697
@two697 Жыл бұрын
Hey, could you start posting more videos about hard or difficult medium problems if you get the time? I think questions like leetcode 587 and 834 are really interesting
@vour6654
@vour6654 Жыл бұрын
he is doing the daily questions.
@krateskim4169
@krateskim4169 Жыл бұрын
Awesome recess
@werstef8681
@werstef8681 7 ай бұрын
hmm, help me if I am wrong but isnt that a combinations of n taken 2 and then divided by 2? mathematically it seems correct but 4 cases give me incorrect. can you help me?
@mokoboko2482
@mokoboko2482 Жыл бұрын
legendary
@shreyaschaudhary-r6d
@shreyaschaudhary-r6d 11 ай бұрын
the second solution is better imo!
@kamalsinghbisht1964
@kamalsinghbisht1964 Жыл бұрын
Time Complexity - O(N) Space Complexity - O(1) Solution : "class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: n = len(nums) good_pair = 0 nums.sort() i ,j = 0, 1 while j < n : if nums[i] != nums[j] : x = j - i - 1 good_pair += x*(x+1)//2 i = j j = j+1 x = j - i - 1 good_pair += x*(x+1)//2 return good_pair "
@yfjsdgzjdnfn
@yfjsdgzjdnfn Жыл бұрын
Sorting takes O(n log n) even if we use sort()
@akhilchauhan9417
@akhilchauhan9417 Жыл бұрын
bruh
Pascal's Triangle II - Leetcode 119 - Python
7:02
NeetCodeIO
Рет қаралды 13 М.
GIANT Gummy Worm #shorts
0:42
Mr DegrEE
Рет қаралды 152 МЛН
«Жат бауыр» телехикаясы І 30 - бөлім | Соңғы бөлім
52:59
Qazaqstan TV / Қазақстан Ұлттық Арнасы
Рет қаралды 340 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 237 М.
Merge Intervals || Leetcode 56 || 1 Variant Question Meta Actually Asks
23:33
Non-Decreasing Array - Leetcode 665 - Python
10:59
NeetCode
Рет қаралды 58 М.
Deepseek Changes Everything
10:26
NeetCodeIO
Рет қаралды 81 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 246 М.
Find the Difference - Leetcode 389 - Python
11:24
NeetCodeIO
Рет қаралды 11 М.
SUBARRAY SUM EQUALS K | LEETCODE 560 | PYTHON SOLUTION
16:32
Cracking FAANG
Рет қаралды 6 М.