1509. Minimum Difference Between Largest and Smallest Value in Three Moves | Heap | Sorting | Greedy

preview_player
Показать описание
In this video, I'll talk about how to solve Leetcode 1509. Minimum Difference Between Largest and Smallest Value in Three Moves | Sorting | Greedy

Let's Connect:

About Me:
I am Aryan Mittal - A Software Engineer in Goldman Sachs, Speaker, Creator & Educator. During my free time, I create programming education content on this channel & also how to use that to grow :)

✨ Hashtags ✨
#programming #Interviews #leetcode #faang #maang #datastructures #algorithms
Рекомендации по теме
Комментарии
Автор

I think there is better approach without using any higher order data structure as below :
int minDifference(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n=nums.size();
int ans=INT_MAX;
if(n<=4){
return 0;
}
else{
for(int i=0; i<4; i++){
int d=nums[n-4+i]-nums[i];
ans=min(ans, d);
}

}
return ans;
}

Thanks for

purushottamnitwarangal
Автор

Thankyou bro i couldn't understand this question earlier but you really make it so simple

varunjain
Автор

Your Teaching Style is Awesome!!!
In love with data structures...
& appreciate your consistency of uploading daily videos.
Keep Going Bruh..
Well

rkpkdmz
Автор

Keep up the good work, your daily videos motivate e to do leetcode daily

cenacr
Автор

salute to your efforts for providing an optimised code as well... <3

italk-gjkk
Автор

class Solution {
public:
int minDifference(vector<int>& nums) {
int n = nums.size();
if(n<=3) return 0;
sort(nums.begin(), nums.end());
int ans= INT_MAX;
int start = 0, end = n-4;
while(end<n){
ans = min(ans, nums[end]-nums[start]);
start++;
end++;
}
return ans;
}
};

naveroo
Автор

bhaiya please explain the binary search approach

santhosh
Автор

Thank you bhai!!
Great explanation!!

manishprasad
Автор

just want to know the difference in time and space complexity between nth element and partial_sort approach in c++ vs this approach afaik nth element in c++ uses partition method of quick sort so i think time complexity is same as this and what about space complexity

bhamidipatisatwik
Автор

bro can we also do the above problem in O(N) Time Complexity ?

ayaaniqbal