Permutations | Leet code 46 | Theory explained + Python code

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • This video is a solution to Leet code 46, Permutations. I explain the question and the best way to solve it and then solve it using Python.
    Comment below if you have a better solution to this problem!
    Let me know if you have any feedback and don't forget to subscribe for more videos!
    Code: leetcode.com/problems/permuta...)
    More leetcode questions solved:
    • Add and Search Word - ...
    Timestamps:
    0:00 Question Explained
    0:42 Solution Explained
    6:01 Python Code
    Gear used:
    Mic: amzn.to/3awi3b6
    Drawing pad: amzn.to/2O7PaaU

Komentáře • 58

  • @rhodabaruch4
    @rhodabaruch4 Před rokem +2

    Thank you for explaining permutations to me! It seems so simple, but it's been the hardest thing to understand. After a long day of procrastinating and not really feeling like coding, I'm glad I watched this! I learned something new and understand this problem which I have been running my wheels on for a WHILE now! thank you!

  • @jothi1018
    @jothi1018 Před rokem +3

    way better explained and coded than any other video I could find, thank you

  • @allaboutthementality1594
    @allaboutthementality1594 Před 3 lety +6

    I dont know how someone could dislike this, kinda crazy. Your videos are amazing. Nice to listen to and easy to understand, keep up the great work! You are helping so many of us!

  • @BogdanHladiuc
    @BogdanHladiuc Před 3 lety +9

    I found your channel recently and to be honest it's one of the best when it comes to solving and explaining the solutions. I really enjoying it even though python is not my thing.

  • @michellemasters7221
    @michellemasters7221 Před 2 lety +2

    Thank you, this is the first explanation that actually made sense to me!

  • @yousifsalam
    @yousifsalam Před rokem

    Thank you so much for the video!
    Here's your code, just a bit cleaner:
    class Solution:
    def permute(self, nums: list[int]) -> list[list[int]]:
    res = []

    def backtrack(nums, path):
    if not nums:
    res.append(path)
    for i in range(len(nums)):
    backtrack(nums[:i]+nums[i+1:], path + [nums[i]])

    backtrack(nums, [])
    return res

  • @MP-ny3ep
    @MP-ny3ep Před 2 lety

    This is one of the simplest and most comprehensive solutions i've ever come across pertaining to this problem . Thank you

  • @davezhang8314
    @davezhang8314 Před 3 lety +1

    Your vids are so helpful that I find myself specifically picking mediums/hards that you've covered!

  • @nunzioruffo3645
    @nunzioruffo3645 Před 10 měsíci

    Great explanation!

  • @manjultripathi8309
    @manjultripathi8309 Před 8 měsíci

    Great Explanation🙏

  • @TheElementFive
    @TheElementFive Před 2 lety +1

    How does [ 3, 2 ] become in our scope? With nums[:x] + nums[x + 1:] I get how we get [2, 3], but I just can't visualize how we can get [3, 2], [3,1], etc.. (basically anywhere where the permutation is in an order that is different than that of the input array.

  • @cgqqqq
    @cgqqqq Před 2 lety

    omg awesome bruh, you are the best since you are the only one treat us like *we know nothing* 😂

  • @adhiradeogade5523
    @adhiradeogade5523 Před 3 lety

    Perfect! Thanks, Sai.

  • @ScreechOwlx
    @ScreechOwlx Před 3 lety +1

    Excellent clarity of communication

  • @godlike33
    @godlike33 Před 3 lety +2

    Bro u r awesome ! I'm when I first try to solve this question I'm like blank I can't find a approach and now I'm laughing on myself 😂 bro u r awesome 🔥🔥

  • @user-op1xk2yz8d
    @user-op1xk2yz8d Před 10 měsíci

    Thanks...That was so helpful

  • @heisenberg1844
    @heisenberg1844 Před 3 lety +1

    Thank you! Well explained.

  • @mikijane4714
    @mikijane4714 Před rokem

    amazing video 😭

  • @jeffjames15
    @jeffjames15 Před 3 lety +3

    Thanks for the video. Could you explain how the recursion in the last function makes the nums[] empty? I am always very confused about recursion.

  • @ordosolis
    @ordosolis Před rokem

    Thank you sooooooo much for this video! It’s really helpful. I felt so stupid in class as I didn’t understand it. but your explanation was so clear 🙏 a million thanks yous for uploading this video

  • @ikrenji8125
    @ikrenji8125 Před 2 lety

    very nice solution ~ the other solutions i saw for permutations were bending my mind, but this one is super clear. thx

  • @saketthavanani312
    @saketthavanani312 Před 3 lety +1

    Great Explanation!!!!

  • @pritikaur4974
    @pritikaur4974 Před rokem

    thank you for such an amazing explanation!!!!

  • @piyushupadhyay8361
    @piyushupadhyay8361 Před 2 lety

    amazing crystal clear solution....thankyou for explanation

  • @lavanyam3224
    @lavanyam3224 Před 3 lety

    Wow just awesome!!! This channel is gonna blow up someday :)

  • @bhavyaseth4254
    @bhavyaseth4254 Před 2 lety

    Can you please tell a solution using this approach only but not using slicing. How can this be implemented using Append and Pop

  • @tianmingguo8271
    @tianmingguo8271 Před 2 lety

    great solution!

  • @mathiangchot3784
    @mathiangchot3784 Před 3 lety

    Is it possible to generate all possible differences between a pair of numbers in a list: e.g., lst = [1, 2, 3, 4]?

  • @stephenchu9106
    @stephenchu9106 Před 3 lety +2

    Thanks for the video! Could you go over the time and space complexity of your solution? I think the space complexity is O(N^2) because the stack will reach a depth of N, and each call contains a nums and path variables which can each be of size N. I think the time complexity is (N!*N) because there will N! calls and each call requires creating the nums and path variables, which each can be up to size N

    • @saianishmalla2646
      @saianishmalla2646  Před 3 lety +1

      Thank you for explaining the time and space complexity!

    • @ritikmishra8771
      @ritikmishra8771 Před 3 lety +1

      the sc is O(n^2*n!) because we call the path(O(n)) and nums(O(n)) (" n! times "and we store path and nums everytime calling the recursive function (backtracking)

    • @abdulraheemsiddiqui9536
      @abdulraheemsiddiqui9536 Před rokem

      Isn't the time complexity n^2 * n! because there will be n! calls and each call iterates over nums and then create nums so n^2 ?

  • @utkarshsaboo
    @utkarshsaboo Před 2 lety

    Hi Sai thank you so much for the video! This really helped me make my solution fast. I do have a question for you, I was initially using copy.deepcopy to create a new copy of the "nums" array and a new copy of the "path" array and passing it into each recursive call (after the necessary modifications, like removing the current element from nums). However that solution was extremely slow. I see that you do the exact same thing but with nums[:x]+nums[x+1:] && path+[nums[x]] respectively ~ can you tell me why this solution is so much faster than what I was doing?

  • @sampritaroychoudhury2013

    How to get p permutations of n numbers? Like 5 permutations from 10 random numbers

  • @lilithakobyan7716
    @lilithakobyan7716 Před 3 lety

    Thanks!

  • @acehanks
    @acehanks Před 3 lety

    Thanks for the explanation

  • @kinfish1365
    @kinfish1365 Před rokem

    Soooo good

  • @mengxuetang5298
    @mengxuetang5298 Před 3 lety

    I love watching your video and please come on!

  • @devin12428
    @devin12428 Před rokem

    Thx for the video, is the solution iterative or recursive?

  • @sudharshan3863
    @sudharshan3863 Před 3 lety +1

    Path=[1,2,3] then it reaches a base case and path will be append to the result list.What will be next step?

    • @saianishmalla2646
      @saianishmalla2646  Před 3 lety

      so after reaching [1,2,3] then we would go back a step getting us to the second position and we try the remaining combination which in this case would be [1,3,2] after this we tried all combination for the second and third spot so we go back to the very first spot giving us [2,1,3] and so and until we get all combinations.

    • @sudharshan3863
      @sudharshan3863 Před 3 lety

      @@saianishmalla2646 thank you😌

  • @ruthwang9177
    @ruthwang9177 Před 3 lety +1

    Your explanation is very clear and explicit!

  • @murthymurthy9606
    @murthymurthy9606 Před 2 lety

    I want support in python can you help me?

  • @lukenick2299
    @lukenick2299 Před 2 lety

    This solution as is doesn't work. Copy and paste it to see. Structure of result etc is broken

  • @stevenspens6505
    @stevenspens6505 Před 3 lety

    try this on n=10^6 lol