Next Permutation - LeetCode 31 - Python [O(n) time and O(1) Space!]

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ธ.ค. 2024

ความคิดเห็น • 76

  • @shubhamh7451
    @shubhamh7451 3 ปีที่แล้ว +6

    With the first algorithm, i was like, damn, I built the exact same logic but it did not work on all cases. But then with the slightly different approach, it works. Thank you, keep up!

  • @Ankit-hs9nb
    @Ankit-hs9nb 2 ปีที่แล้ว +8

    Another great video!
    we can use this logic maybe for swapping:
    nums[ind1] , nums[ind2] = nums[ind2] , nums[ind1]

  • @jlozano1
    @jlozano1 4 ปีที่แล้ว +2

    For some reason, the first approach works on some cases but on others it doesn't. The second approach (5:11) worked great. Thanks!

  • @amanshaukat1105
    @amanshaukat1105 3 ปีที่แล้ว +3

    you earned a new subscriber....................your code writing skill is very nice, ..... it's clean and understandable............definitely will try to imitate you

  • @swagatpatra2139
    @swagatpatra2139 4 ปีที่แล้ว

    impressive coding skills. The accent though helped me understand better.
    the same thing in java
    // [1,7,9,9,8,3]
    // index = 1
    // sort from index 3 till end
    // swap 7 with just larger to right
    public void nextPermutation(int[] nums) {
    int n = nums.length;
    if(n0){
    if(nums[i]>nums[i-1]) {
    index = i-1;
    break;
    }
    i--;
    }
    if(index == -1) reverseSort(nums, index+1, n-1);
    else{
    reverseSort (nums, index+1, n-1);
    for(i = index+1; inums[index]) {
    swap(nums, index, i);
    break;
    }
    }
    }
    }
    void swap(int[] arr, int a, int b){
    int temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
    }
    void reverseSort(int[] arr, int start, int end){
    while(start

  • @nikhilmishra7572
    @nikhilmishra7572 4 ปีที่แล้ว +1

    Straight to the point and easy to understand. Thanks!

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว

      Ty!:))

    • @nikhilmishra7572
      @nikhilmishra7572 4 ปีที่แล้ว +1

      @@DEEPTITALESRA Please make more leetcode videos

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว +1

      @@nikhilmishra7572 yassss more coming up!!:)

    • @nikhilmishra7572
      @nikhilmishra7572 4 ปีที่แล้ว

      @@DEEPTITALESRA can you please also explain Leetcode question 775. Global and Local Inversions
      ?

  • @mydalaharsha3132
    @mydalaharsha3132 2 ปีที่แล้ว +1

    very well explained, thank you so much for the solutions. I want to know how did you start learning DSA. I'm switching to software engineering from different background, if there is any good book that you followed while preparing can you let me know please

  • @suvrobanerjee2399
    @suvrobanerjee2399 2 ปีที่แล้ว +1

    Thanks, for the clear explanation.

  • @tejeshwararaothella8761
    @tejeshwararaothella8761 2 ปีที่แล้ว

    you can assign a variable for lenght of array,which saves computational power

  • @samsonkolge3627
    @samsonkolge3627 3 ปีที่แล้ว +2

    For swapping you could do it in one line in python: i1, i2 = i2, i1

    • @DEEPTITALESRA
      @DEEPTITALESRA  3 ปีที่แล้ว

      Yes! The underlying logic is the same as using temp, but you're right it makes for cleaner code!!:)

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII 3 ปีที่แล้ว

    can you explain the ancient indian method intuition behinfd the next_permutation stl in c++?

  • @lukt
    @lukt 2 ปีที่แล้ว

    Great explanation, thank you! :) One thing I also found is that we don't necessarily need to check if next_num is less than len(nums) in line 28, since there must be a number larger than nums[dec], otherwise we would have a returned in line 26 earlier.

    • @DEEPTITALESRA
      @DEEPTITALESRA  2 ปีที่แล้ว +2

      Ahh I totally missed that, you are absolutely correct! - line 28 should just be:
      "while nums[next_num]

  • @divyatalesra4767
    @divyatalesra4767 3 ปีที่แล้ว +1

    Really great!!! Thanks so much!!! Wow!!!

  • @jeremyliang7159
    @jeremyliang7159 3 ปีที่แล้ว

    Awesome explanation! Finally understood this question! Thanks!

    • @DEEPTITALESRA
      @DEEPTITALESRA  3 ปีที่แล้ว

      Thank you so much Jeremy!! Love to hear that:)

  • @nooshinyousefi8150
    @nooshinyousefi8150 3 ปีที่แล้ว

    You are so good in explaining, please upload more post and more questions

    • @DEEPTITALESRA
      @DEEPTITALESRA  3 ปีที่แล้ว

      Thanks so much Nooshin!! More on the way:))

  • @shaharrefaelshoshany9442
    @shaharrefaelshoshany9442 3 ปีที่แล้ว +1

    amazing amazing, best ever

  • @mridul1075
    @mridul1075 2 ปีที่แล้ว +2

    wow explanation

  • @linglin9616
    @linglin9616 3 ปีที่แล้ว

    Very good explanation! Easy to understand! Thank you very much!

  • @prithviraj8801
    @prithviraj8801 4 ปีที่แล้ว

    I saw other videos on this question but i wasnt able to understand.then
    Watched your video and understood in first go...great work ..keep it up

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว

      So glad to hear this helped!:) tysm!!

  • @devedroy
    @devedroy 2 ปีที่แล้ว

    a lil confusion on how you found the next greater number after reversing the right part of the array.

  • @praweenkr01
    @praweenkr01 2 ปีที่แล้ว

    just_great thanks you sharing. pls continue making more such videos

  • @PhoenixRisingFromAshes471
    @PhoenixRisingFromAshes471 3 ปีที่แล้ว

    GREAt your second approach is on fire......the first approach has a problem in dealing with duplicates . If the next greater element of arr[index-1] is duplicated then we need to swap with the rightmost only because if we do not then after reversing ,smaller element will move furthur to the least significant positions which is wrong.......ABSolutely spot on

  • @johnwick2018
    @johnwick2018 3 ปีที่แล้ว

    we can swap values without using temp right?
    a, b = b, a

    • @DEEPTITALESRA
      @DEEPTITALESRA  3 ปีที่แล้ว

      Yes! It's the same underlying logic but you're right John - that would definitely make for cleaner code!

  • @anujkhare3815
    @anujkhare3815 2 ปีที่แล้ว

    thanks a lot it helped me so much, but here's one suggestion though, i think you must have spent a little bit more time on cases where digits were repeating.
    i appreciate you taking the example - { 1 ,7 ,9 ,9 ,8 ,3} .I guess this example was really significant because this is one of the cases which people tend to forget ( that we might encounter digits repeating).
    Another great example could be taking - {1,7,9,9,8,7,3} and these two examples would cover both the = sign in comparisons.
    Anyways , great explanation.
    thankyou

  • @RohitKumar-ck5tt
    @RohitKumar-ck5tt 3 ปีที่แล้ว +1

    great mam, wonderful explaination plz upload more videos in python

    • @DEEPTITALESRA
      @DEEPTITALESRA  3 ปีที่แล้ว +1

      Thank you so much Rohit!! More videos coming soon:))

  • @朱超-c7c
    @朱超-c7c 4 ปีที่แล้ว

    this is great, very clear to understand

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว

      Thank you sm!:)

    • @朱超-c7c
      @朱超-c7c 4 ปีที่แล้ว

      @@DEEPTITALESRA i just get started with leetcode, please post more videos

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว

      朱超 more on the way!:)

  • @jazabc.9014
    @jazabc.9014 4 ปีที่แล้ว +2

    Damn I really struggled with this question. Did you solve it on your first try without looking at hints/solution ?

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว +8

      Hey Jazab! tbh I did this question a while ago, but I'm almost certain I used hints for this problem - it's very nuanced and not immediately obvious - no worries at all if you struggled with it. I think just the fact that you tried a question you had not seen before and learned of the solution (whether on your own or using help/watching videos) is really helpful since you just learned something new! And going forward you now know how to solve this question and other similar ones:)

  • @codedoctor3265
    @codedoctor3265 3 ปีที่แล้ว

    I tried to simplify code
    ```
    class Solution(object):
    def nextPermutation(self, nums):
    i=len(nums)-2
    while i>=0 and nums[i]>=nums[i+1]:
    i-=1
    if i>=0:
    j=len(nums)-1
    while j>=0 and nums[j]

  • @tyanay7341
    @tyanay7341 3 ปีที่แล้ว

    Nice explanation! But don't you think we can remove those explicit conditions for length 1 & 2.

    • @DEEPTITALESRA
      @DEEPTITALESRA  3 ปีที่แล้ว

      yes def! I had just included that to show base cases to consider, but you are absolutely correct - it'll make for much cleaner code!:)

  • @amishanegi6465
    @amishanegi6465 3 ปีที่แล้ว

    pls provode the detailed explanation of what u have done in the last 4 lines

    • @alwayssporty8102
      @alwayssporty8102 3 ปีที่แล้ว

      same im zoning out those 4 lines

    • @DEEPTITALESRA
      @DEEPTITALESRA  2 ปีที่แล้ว

      ​ @always sporty ofc!! So in the last lines, after we have finished reversing, we want to make the next permutation, and in order to do that we want to find the correct placement for our current number "next_num". @5:36 for example, we want to place the number so we maintain the ascending order for the numbers we just finished reversing. This will form the smallest next number possible, which would be what we are looking for - the next permutation. Looking from @1:03 might help with the walk-through of the different examples and why we want to do that:), but if you have any questions at all let me know and I'll be more then happy to answer Amisha!:))))

  • @manangupta1789
    @manangupta1789 4 ปีที่แล้ว

    Thanks a lot, Great explanation!

  • @karundonga9413
    @karundonga9413 4 ปีที่แล้ว +2

    plzz make more vidoes they helped me a lot XD

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว +1

      awww thank you sm!! I'm really glad it helped:) ill def be making more videos soon (once classes dial down again haha)

  • @AnwarMohammad-w2l
    @AnwarMohammad-w2l ปีที่แล้ว +1

    thanks

  • @sagarkansara4388
    @sagarkansara4388 4 ปีที่แล้ว

    I don't know why but I am getting a runtime error in Python2.

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว

      oo hmm I tried running it in Python2 rn and it seemed to work? Do you happen to know which part is timing out by any chance?

  • @mangeshgupta5677
    @mangeshgupta5677 4 ปีที่แล้ว

    great explanation thank you

  • @pavanilla4374
    @pavanilla4374 4 ปีที่แล้ว

    very clear and nice explanation tq :)

  • @Kushagra_21
    @Kushagra_21 4 ปีที่แล้ว

    Thanks a lot for this.

    • @DEEPTITALESRA
      @DEEPTITALESRA  4 ปีที่แล้ว +1

      Ofc!!:)

    • @Kushagra_21
      @Kushagra_21 4 ปีที่แล้ว

      @@DEEPTITALESRA Could you please upload a video on " First missing positive number in an array" in O(1) space and O(N) time.

  • @ashishranjan8297
    @ashishranjan8297 4 ปีที่แล้ว

    Hi Deepti , loved your video . Would love to collaborate and make a code review , sys design , algo solving -live videos . Let me know if you would like to collaborate sometime.

  • @sujaysreedhar8313
    @sujaysreedhar8313 3 ปีที่แล้ว

    Damnn! What an algorithm!

  • @omfuke3083
    @omfuke3083 3 ปีที่แล้ว

    thanks didi

  • @shadik000
    @shadik000 4 ปีที่แล้ว +1

    you are my crush..❤️❤️❤️❤️

  • @abhijeetkumar2204
    @abhijeetkumar2204 4 ปีที่แล้ว +2

    nice explanation...are u indian?

    • @DEEPTITALESRA
      @DEEPTITALESRA  3 ปีที่แล้ว

      Thank you Abhijeet!! And yep I am!:)

  • @deepaktalesra5443
    @deepaktalesra5443 3 ปีที่แล้ว

    Awesome Deepti