Leetcode - Max Number of K-Sum Pairs (Python)

preview_player
Показать описание
January 2021 Leetcode Challenge
Leetcode - Max Number of K-Sum Pairs #1679
Рекомендации по теме
Комментарии
Автор

great video, i was also confused by the description. "remove them from the array" followed by "Return the maximum number of operations you can perform on the array." seems they only wanted to latter, and i solved this with a two pointer. thank you for the help!

tarsco
Автор

Great video man!
can you please write in the description of your videos the time and space complexity it would really help!

avichai
Автор

The edge cases threw me for a fit on this problem! I didn't think about using a seen set - that was cool! I just updated the hashmap in place. Thanks Tim!

counts = Counter(nums)
pairs = 0
for num in nums:
#find the complement
complement = k - num
if complement in counts:
#edge case 1, enough elements
if counts[num] > 0 and counts[complement] > 0:
#edge case when they are the same and count at nums < 2
if num == complement and counts[num] < 2: #we need two of the same
continue
pairs += 1
counts[num] -= 1
counts[complement] -= 1
return pairs

janmichaelaustria
Автор

Hi, thanks a lot for your videos. You make problems look very simple. Could you please do videos for Leetcode weekly contest questions ?

pc
Автор

Good video bro, next time tho maybe slow down a little bit. 😀

thatguy