Minimum Difference Between Largest and Smallest Value in Three Moves | 2 Approaches | Leetcode 1509
Вставка
- Опубліковано 25 сер 2024
- Whatsapp Community Link : www.whatsapp.c...
This is the 95th Video of our Playlist "Arrays 1D/2D : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a good Array problem : Minimum Difference Between Largest and Smallest Value in Three Moves | 2 Approaches | Easy Explanations | Leetcode 1509 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Minimum Difference Between Largest and Smallest Value in Three Moves | 2 Approaches | Easy Explanations | Leetcode 1509 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) : github.com/MAZ...
Leetcode Link : leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZ...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach 1: Using Complete Sorting
Time Complexity: O(nlogn)
Space Complexity: O(1)
Description:
Sorting: The entire array nums is sorted.
Edge Case: If the array size is 4 or less, the minimum difference is 0 because we can remove all elements.
Calculate Minimum Difference: The minimum difference is calculated by considering four possible cases:
Removing the three largest elements.
Removing the three smallest elements.
Removing the two largest and one smallest element.
Removing the two smallest and one largest element.
Result: The minimum of these differences is returned as the result.
Approach 2: Using Partial Sorting
Time Complexity: O(n)
Space Complexity: O(1)
Description:
Edge Case: If the array size is 4 or less, the minimum difference is 0 because we can remove all elements.
Partial Sorting:
Use partial_sort to sort the first four elements to find the four smallest elements.
Use nth_element to position the n-4th largest element correctly, such that all elements before it are smaller and all elements after it are larger.
Sort the last four elements to ensure the four largest elements are in their correct positions.
Calculate Minimum Difference: Similar to Approach 1, calculate the minimum difference by considering the same four possible cases.
Result: The minimum of these differences is returned as the result.
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
00:00 Intro & explanation
02:30 Approach 1 -Complete Sorting
19:30 Approach 2 -Partial Sorting
Your way of teaching is great.
Thanks !
Aap ka alawa aur koi bhi accha nehi hai, Jo beginners se samjata hai
Aap ka hi Wait tha
yaar mast samjhate ho aap. ekdm maza hi ajata hai
Was waiting. Thank you
king of best explanations.
SUCH A BEGINNER FRIEDNLY AND EFFECTIVE WAY !!! KUDOS
Voice increased wow thanks
Thanks to your arrays playlist.. was able to solve it ❤
bhaiyya aagaya..
I was very happy to find out that you are my college alumni from IIEST, Shibpur. Your videos help me a lot, and I have a great respect and love for you. ❤️
thank you bro , nice video !!
Thank you bhaiya. Bhaiya as Segment tree playlist is going on bhaiya please solve some segment tree problems from Codechef also. Codechef contains good quality problems on segment tree.
Thanks man. Finally understood.
class Solution {
public:
int minDifference(vector& nums) {
sort(nums.begin(),nums.end());
int n=nums.size();
if(n>=4){
return min({nums[n-4]-nums[0],nums[n-1]-nums[3],nums[n-3]-nums[1],nums[n-2]-nums[2]});
}
return 0;
}
};
aap hi ka wait tha. subah se referesh kar raha tha
last 3 years mein pehli baar partial sort bhi kuch hota hai krke pta laga.
Bhaiya please launch your DSA Sheet for ultimate interview preparation. By the way Got rejected in DE Shaw OA today. 😢
Learning ++
Thanks a lot bhaiya ❤❤ learnt something new today partial_sort and nth_element
Congrats for 55k subs🥳🥳
thankyou sir
please make one video on leetcode 687 please see that question once...
The hit and trial method came to my mind after i read the problem, but i kept on thinking that cant be the solution. I face this many times and try top get the optimzed solution from the beginning. How to deal with this?
Bhaiya binary search vala approach bhi discuss karna please muje lag raha jaise Koko and bananas kiya same ye bhi karskate hai par Mera kuch test case pass nahi horahe
Bhaiya can you please add pdf notes of graph concepts and questions to github.
Thanks for the video sorting takes also space o n right?
bhaiya priority queue wala solution bhi bata do
Sir could you please speak in english .I am a south indian.You are really doing a great job .Your explaination and step by step approach is top notch.But south indian students cant understand hindi very well.Please do your videos in english so that everyone can understand sir. Its a sincere request from my side 🥹🥹
public int minDifference(int[]nums){
int n=nums.length;
if(n
i tried to solve the problem by making an approach by myself ,but the approach came out to be limited to some cases everytime .
give me some suggestions to improve the problem solving skills!!
hi what if the questions had said K instead of 3... how would we approach then ?
res = Math.min(res, nums[n-1] - nums[3]);
res = Math.min(res, nums[n-2] - nums[2]);
res = Math.min(res, nums[n-3] - nums[1]);
res = Math.min(res, nums[n-4] - nums[0]);
loop laga kar karna hoga then..
I guess..
ek start se and ek end se...
MIK bhaiya nhi sir ho gye hai @7.22 seconds,
No no. “No sir” . Only bhaiya 😇🙏❤️
We all are on same level ❤️❤️❤️
kuch samajh nhi aaya bhai
Hello there,
Apologies if you felt so.
Would you like to point which point exactly you felt lost. I will try my best to help here
Second approach was bit unnecessary.
Yep I agree.
Just felt like sharing 😇
I will try to avoid adding similar approaches in a video.
Thank you for the feedback ❤️🙏😇
Kuch smjh me nhi aaya aaj toh!
Hi there,
Would you point where exactly you felt stuck. I will try my best to help here ❤️