Next Permutation || LeetCode #31 || Medium || Code + Explanation || C++

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 49

  • @manpreetsinghrana7751
    @manpreetsinghrana7751 2 роки тому +11

    For all those who are getting confused about the usage of the min variable in the code. It is actually not needed at all.
    The thing is, in the second part of algo, when we are moving from infpt to the end of the array and comparing every element with toswap value, it is by default that everything after inftp position is already in descending order, so the algorithm in step 2 will automatically swap with the next larger element from toswap value. Take this test case [2,7,5,3,1] and visualize it by writing down each step.

  • @Raj10185
    @Raj10185 Рік тому +4

    Better version of code with no confusion :-
    void nextPermutation(vector& nums) {

    // Let,s try to find out the inflation point and alwz find from the back
    // which is firstpoint from back which breaks the rule
    // to swap point is just left index of inf point

    int n = nums.size();
    int infpt = -1;
    int toswap = -1;

    for(int i = n-1 ; i>=0 ; i--)
    {

    if( i>0 && nums[i-1] < nums[i])
    {

    infpt = i;
    toswap = i-1;

    break;

    }



    }

    if(infpt == -1) //that means order is correct just need to reverse to get next permutation
    reverse(nums.begin(),nums.end());
    else
    {

    // if we found the inflation point now we need to find the candidate no which we have to swap

    //lets go from back till inf point and find out the first no which is greater

    for(int i = n-1 ; i>= infpt ; i--)
    {

    if(nums[i] > nums[toswap])
    {

    swap(nums[i],nums[toswap]);
    break;


    }


    }

    // step 3 => reverse the nos from the inflation point to the last for get the next permutation

    reverse(nums.begin()+infpt,nums.end());


    }



    }
    };

  • @gremlan6419
    @gremlan6419 2 роки тому

    This is the second time I have watched one of your videos and received a clear conise explanation of what needs to be done - thanks again.

  • @rajeevkumararyan8138
    @rajeevkumararyan8138 2 роки тому +3

    class Solution {
    public:
    void nextPermutation(vector& nums) {
    next_permutation(nums.begin(),nums.end());
    }
    };

  • @Arun-zy8wi
    @Arun-zy8wi Рік тому

    Leave the answer that anyone can find it..The most important thing is you made me understand the question quickly. Thank you

  • @sathishraj1
    @sathishraj1 2 роки тому +1

    This helps me much. Please do a series on Dynamic Programming, It would be helpful for some xperienced professional like me who wanted to crack Product companies

  • @BoostedPLAYER1
    @BoostedPLAYER1 2 роки тому

    def solve(nums):
    find idx1
    find idx2
    swap(nums[idx1],nums[idx2])
    reverse(nums[idx1+ 1 till end)

  • @harshitbhatt6822
    @harshitbhatt6822 2 роки тому +8

    why arent we updating min inside the if statement?

  • @AffairWithGeo
    @AffairWithGeo 10 місяців тому

    I hope i could add an audio message. . .The simple and execution is wow...looked across entire youtube. but this one is 😌

  • @ayushpathak2503
    @ayushpathak2503 3 роки тому +14

    please tell me you are not updating the min then how u r getting the correct ans....??

    • @gauthampkrishnan6118
      @gauthampkrishnan6118 2 роки тому

      exactly how does min work? even if you remove the min statement its working

    • @maxxiegodbole9830
      @maxxiegodbole9830 2 роки тому

      exactly

    • @ayushpathak2503
      @ayushpathak2503 2 роки тому

      @@arshdeep011 exactly without that also u will get the same result.

    • @AishwarysinghEC
      @AishwarysinghEC 2 місяці тому

      due to limited test cases on leetcode ,we need to modify the code a little bit for sure to update min

  • @AishwarysinghEC
    @AishwarysinghEC Рік тому +1

    awesome explanation

  • @PIYUSH-lz1zq
    @PIYUSH-lz1zq 3 роки тому +1

    Perfect explanation of code 👌

  • @Ashish-yf1wc
    @Ashish-yf1wc 2 роки тому +1

    Nicely explained 🙂🙂

  • @SurajKumar-ng5xu
    @SurajKumar-ng5xu Рік тому

    how is this algo working for three digits numbers ? can u please explain?

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 2 місяці тому

    NICE SUPER EXCELLENT MOTIVATED

  • @AnishKumar-fr4ye
    @AnishKumar-fr4ye 2 роки тому +1

    Why did you not use swap function to swap nums[j] and nums[infpt-1]....... i was using the swap function , the progam was showing error, but when I manually did it..... it got accepted .. can you please explain?

    • @vanshsharma-lg1hj
      @vanshsharma-lg1hj 9 місяців тому +1

      may be these function are applicable on vectors only...not on the array.

  • @rohithbhandari7836
    @rohithbhandari7836 11 місяців тому

    What if nums contains 0 as element?

  • @c.ramanji2604
    @c.ramanji2604 Рік тому

    You are the best mam. Thank you so much😊😊

  • @shreyasatyakam2641
    @shreyasatyakam2641 Рік тому

    why did you run the first loop backwards?

  • @AvinashKumarJha-c8h
    @AvinashKumarJha-c8h Рік тому

    how we can say this algorithm take O(N) time. we all now that sort function take O(NlogN). ???

  • @asthakulshrestha9250
    @asthakulshrestha9250 2 роки тому

    address sanitizer error is coming in leetcode. why?

  • @pikachu3686
    @pikachu3686 2 роки тому

    Line 37: Char 18: error: no member named 'nextPermutation' in 'Solution'; did you mean 'nextPermuatation'?
    Solution().nextPermutation(param_1);
    ^~~~~~~~~~~~~~~
    nextPermuatation
    Line 4: Char 8: note: 'nextPermuatation' declared here
    void nextPermuatation(vector & nums){
    ^
    1 error generated.
    WHAT WAS THAT

  • @satyamshukla7922
    @satyamshukla7922 Рік тому

    can anyone tell me we swapped with 3 or 5 in the above code.
    for the first time iteration we swapped nums[j], with nums[infn-1]

  • @Idukhan-jj9kc
    @Idukhan-jj9kc 3 роки тому

    Tq so much eagerly we r waiting yr next video

  • @pushkarsingh8369
    @pushkarsingh8369 2 роки тому

    U explain very well

  • @shreyaesha001
    @shreyaesha001 2 роки тому

    please explain me why line no. 26?

  • @singhji4149
    @singhji4149 2 роки тому

    Fantastic explanation 👍

  • @danussh
    @danussh 3 роки тому

    It was worth subscribing your channel😀😁

  • @SaumyaSharma007
    @SaumyaSharma007 3 роки тому +1

    Thanks a lot Di....👏

  • @foodcourt6810
    @foodcourt6810 2 роки тому +1

    I can't understand this problem can anyone explain?

  • @mohammadanaskhan8364
    @mohammadanaskhan8364 Рік тому

    No need of min
    Jus to this-
    for(int j=inftp ; j

    • @AbhishekKumar-gn4mg
      @AbhishekKumar-gn4mg 9 місяців тому

      i think this reply is helpful who has doubt in line 26

  • @narendra7556
    @narendra7556 2 роки тому

    what is the time complexity for solving this?

  • @meandpenguinvlogs8221
    @meandpenguinvlogs8221 2 роки тому

    💯💯

  • @samreenjahan2482
    @samreenjahan2482 2 роки тому

    nice explanation

  • @thesilentcoder828
    @thesilentcoder828 3 роки тому

    just wow