Since the while loop runs 𝑛 / 𝑔 𝑟 𝑜 𝑢 𝑝 𝑆 𝑖 𝑧 𝑒 n/groupSize times and each iteration of the for loop involves groupSize operations, each taking 𝑂 ( log 𝑛 ) O(logn), the total time complexity of this part is: 𝑂 ( 𝑛 groupSize × groupSize × log 𝑛 ) = 𝑂 ( 𝑛 log 𝑛 ) O( groupSize n ×groupSize×logn)=O(nlogn)
@codestorywithMIK8 ай бұрын
My bad. Thank you for the correction ❤️
@gui-codes8 ай бұрын
thanks
@CodeMode93138 ай бұрын
@@codestorywithMIK its ok no issue
@DevOpskagyaan8 ай бұрын
Thanks man
@prakharrohra83658 ай бұрын
O(n^2*logn)
@amitchoraria57378 ай бұрын
I dont know if its correct but i see one more optimization that is no freq can be greater than the number of groups that can be formed. So while creating the map itself we can prune few test cases where the freq goes beyond num of groups that is hand.size()/groupSize
@codestorywithMIK8 ай бұрын
Ah yes. That’s a good observation 👍🏻❤️
@darkwarrior676715 күн бұрын
Those comparisons will take more time than the time it saved
@nishantsingh18348 ай бұрын
I think the second loop in which we are checking that a group can be made or not, has a time comp of n*logn and not n*groupsize , as inner loop will move till groupsize and outer will move till n/ groupsize and we are deleting some elements in map which will take logn so overall complexity becomes n*logn
@nishantsingh18348 ай бұрын
As n*groupsize in worst case can lead to n2 as groupsize can be n
@PradeepKumarIIITD8 ай бұрын
Mik bhai solution daal ke hi sote ho.. loved your explaination ab to 50k ho gaye... face reveal ho jayee..
@arthashastra_analyze8 ай бұрын
Hello sir I have seen many of your videos your explanation is amazing 🙏💯💯
@AkashKumarTiwary-u4b8 ай бұрын
Woke up and solved this one, feels good ki optimal solution likha tha 😢
@abhinay.k5 ай бұрын
thank your sir
@abhishektripathi39048 ай бұрын
I solved it without using map with O(1) extra space My solution:- class Solution { public: bool isNStraightHand(vector& hand, int groupSize) { int n = hand.size(); if(n % groupSize != 0) return false; sort(hand.begin(),hand.end()); int i = 0, j = -1, prevCard = -1, count = 0; while(i < n){ if(hand[i] < 0) i++; //if hand[i] is -ve, it means the card is already in some other group else if(prevCard != -1 && hand[i] - prevCard > 1) return false; //Checking if cards are consecutive or not else if(hand[i] == prevCard){ if(j == -1){ //first duplicate found, store it's index in j, next group will start from this index/element j = i; } i++; } else{ if(count < groupSize){ prevCard = hand[i]; hand[i] = -1 * hand[i]; //convert to -ve to denote that card is picked count++; i++; } if(count == groupSize){ prevCard = -1; count = 0; if(j != -1){ //move back to the duplicate you left back and start forming the new group from there i = j; j = -1;//Assign j back to -1 to store future possible duplicate element's index } } } } return count % groupSize == 0; //count should be either 0 or groupSize } };
@ryanrenjith35048 ай бұрын
I had the same idea and approach but I just could not translate it to code. Or atleast I was too lazy today to figure out all those loops! But thanks for the final code!
@TusharRaja30098 ай бұрын
Sir aise questions mein constraints given :--- hands[i]
@prajnanammohnty9508 ай бұрын
taki koi banda 10^9 ka array declare na karde memory limit exceeded error aajayega.
@TusharRaja30098 ай бұрын
@@prajnanammohnty950 accha thanks
@DevOpskagyaan8 ай бұрын
You are awesome man
@kapilnitb8 ай бұрын
Great explanation
@adityaraj-zm7zk8 ай бұрын
bhaiya in this question why we use hashmap
@indrajitpal028 ай бұрын
because in c++ std::map stores the values in a sorted fashion and also we can store the frequency of the elements that will provide us an efficient way to count frequency of each card value, . In case you are in python use DefaultDict(), but then you have to sort the array first, because DefaultDict only stores the oder of insertion of the elements and does not sort them, (in python there is not an inbuilt function to insert the elements in a sorted manner) Hope it helps
@haidarmohammad85108 ай бұрын
MY SOLUTION class Solution { public: bool isNStraightHand(vector& hand, int groupSize) { map mp; for(auto h: hand) mp[h]++; while(mp.size()){ auto fek = mp.begin()->first; for(int i=0; i
@CodeMode93138 ай бұрын
IMPORTANT !!!!!! Bhaiya time complexity is little wrong in the video i guess...for upper part its obviously nlogn but in the second part its the while loop and inside that while loop we have for loop ......so my while loop will run for N/groupsize as the for loop will remove the groupsize element everytime . Talking abt the for loop ...its complexity is groupsize*logn.....so overall time complexity for the lower half will be ..... nlogn
@CodeMode93138 ай бұрын
time complexity = ( N / groupSize ) * groupSize * logN = N * logN
@11csepratikshaargulewar718 ай бұрын
Could you explain why it is groupSize* logn for inner- for loop ?
@akashsonar63328 ай бұрын
Correct Time complexity : O( logn + (groupSize)^2 ) logn due to sorting of map. groupSize number of we will iterate in map. Additional groupSize due to finding consecutive sequence in the map. Therefore it turns out to be O( logn + (groupSize)^2 )
@CodeMode93138 ай бұрын
@@11csepratikshaargulewar71 its running for groupsize times and within that u are doing put and remove operations which takes the logn times
@tommyshelby62778 ай бұрын
please also add the optimal solution, as lee215 discussed
@nikhilhaspe27348 ай бұрын
|| Java Solution || class Solution { public boolean isNStraightHand(int[] hand, int groupSize) { int handSize = hand.length; if(handSize % groupSize != 0) return false; TreeMap tMap = new TreeMap(); for(int i=0; i 0){ int currentKey = tMap.entrySet().iterator().next().getKey(); for(int i=0; i
@sarthakchafle18 күн бұрын
You can directly use firstKey() to get the smallest element in a TreeMap
@cosmicthor73308 ай бұрын
bhaeeya kl wala question clear nhi ho rha achchey sey
@insidious_6818 ай бұрын
sir how to traverse in map?
@codestorywithMIK8 ай бұрын
Example : int main() { std::map myMap; myMap[1] = "one"; myMap[2] = "two"; myMap[3] = "three"; // Using iterators for (std::map::iterator it = myMap.begin(); it != myMap.end(); ++it) { std::cout
@visheshpandey90708 ай бұрын
Solved using Map Approach Beats 95% users Code: class Solution { public: bool isNStraightHand(vector& hand, int groupSize) { sort(hand.begin(),hand.end()); unordered_map mpp; for(auto it:hand) { mpp[it]++; } for(int i=0;i0) { int temp=hand[i]; mpp[hand[i]]--; int j=1; while(j
@AryanSingh-ve7dq8 ай бұрын
BRO CAN YOU MAKE THE O(N) STACK SOLUTION OF JUMP GAME 5 PLEASE?
@adityaraj-zm7zk8 ай бұрын
please provide slide also
@mewaconite8 ай бұрын
This question was tough for me, even though I was able to come up with intutiom to solve...lekin i was not able to correctly use map data structure
@AbhishekKumar-zs8ry8 ай бұрын
I solved it but my code beats only 5% of the users 😐
@anonymous29828 ай бұрын
same bro!
@dayashankarlakhotia49438 ай бұрын
public boolean isNStraightHand(int[]hand,int groupSize){ if(hand.length%groupSize!=0)return false; Arrays.sort(hand); for(int i=0;i=0) if(!find(hand,i,groupSize,hand.length)) return false; } return true; } private boolean find(int[]nums,int i,int groupSize,int n){ int f=nums[i]+1; nums[i]=-1; i+=1; int cmt=1; while(i