Max Number of K-Sum Pairs - LeetCode 1679 - Python #leetcode #leetcode75 #2pointers

preview_player
Показать описание
Explaining how to solve Max Number of K-Sum Pairs in Python!

@1:23 - Example + Explanation
@3:30 - Code
@5:34 - Code Walkthrough with Example
@7:37 - Time and Space Complexity

Music: Bensound

Lemme know if you have any comments or questions!:)))

Socials:

Playlists:

Рекомендации по теме
Комментарии
Автор

This is probably a better approach than heaps.

sumanthmurthy
Автор

Sorry, I little bit confused. How it can be as the 2 pointer solution?

math.lovers
Автор

Thank you for uploading video. Hoping to get more and more easy + Medium .

adilansari-hqge
Автор

My solution in C#: Time: O(nlogn) Space O(1)

public class Solution {
public int MaxOperations(int[] nums, int k) {
//int[] result = nums.OrderBy(x => x).ToArray();
Array.Sort(nums);
int i = 0, j = nums.Length-1, count = 0, sum = 0;
while (i < j){
sum = nums[j] + nums[i];
if(sum > k){
j--;
}else if(sum < k){
i++;
}else{
i++;
j--;
count ++;
}
}
return count;
}
}

adnanemezrag
Автор

The decision, in my opinion, is unfortunate. defaultdict is traversed through the array, which is already O(n). Dict itself (as far as I suspect) will rebuild the red-black tree with a large number of numbers. Then we go through the array again in the "for" loop, and this is O(n2). In my opinion, it is cheaper to sort the array - O(logn), and then walk through two pointers, finding pairs along the edges - total O(nlogn)

napeHEK
Автор

Which leetcode you are using are you using an older version of a leetcode if yes then how

ishwarambare
join shbcf.ru