Permutations | Leet code 46 | Theory explained + Python code

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

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

  • @yousifsalam
    @yousifsalam ปีที่แล้ว

    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

  • @rhodabaruch4
    @rhodabaruch4 ปีที่แล้ว +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 ปีที่แล้ว +3

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

  • @allaboutthementality1594
    @allaboutthementality1594 3 ปีที่แล้ว +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 4 ปีที่แล้ว +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.

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

      Thanks you I'm glad these vides are helpful!!

  • @MP-ny3ep
    @MP-ny3ep 3 ปีที่แล้ว

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

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

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

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

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

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

    Excellent clarity of communication

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

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

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

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

  • @TheElementFive
    @TheElementFive 3 ปีที่แล้ว +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.

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

    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

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

    amazing crystal clear solution....thankyou for explanation

  • @nunzioruffo3645
    @nunzioruffo3645 ปีที่แล้ว

    Great explanation!

  • @manjultripathi8309
    @manjultripathi8309 ปีที่แล้ว

    Great Explanation🙏

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

    Your explanation is very clear and explicit!

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

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

  • @godlike33
    @godlike33 4 ปีที่แล้ว +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 🔥🔥

  • @pritikaur4974
    @pritikaur4974 ปีที่แล้ว

    thank you for such an amazing explanation!!!!

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

    amazing video 😭

  • @jeffjames15
    @jeffjames15 4 ปีที่แล้ว +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.

  • @ይኩነኒ
    @ይኩነኒ ปีที่แล้ว

    Thanks...That was so helpful

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

    great solution!

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

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

  • @stephenchu9106
    @stephenchu9106 4 ปีที่แล้ว +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  4 ปีที่แล้ว +1

      Thank you for explaining the time and space complexity!

    • @ritikmishra8771
      @ritikmishra8771 3 ปีที่แล้ว +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 2 ปีที่แล้ว

      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 ?

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

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

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

    Perfect! Thanks, Sai.

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

    Thank you! Well explained.

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

    Great Explanation!!!!

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

    I love watching your video and please come on!

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

      Thank you! I'm not too sure what you mean by the second part.

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

      @@saianishmalla2646 oh sorry, I mean please keep on updating new videos!

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

      haha yes ofc :)

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

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

  • @sudharshan3863
    @sudharshan3863 4 ปีที่แล้ว +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  4 ปีที่แล้ว

      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 4 ปีที่แล้ว

      @@saianishmalla2646 thank you😌

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

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

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

    I want support in python can you help me?

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

    Thanks for the explanation

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

    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?

  • @kinfish1365
    @kinfish1365 ปีที่แล้ว

    Soooo good

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

    Thanks!

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

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

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

    try this on n=10^6 lol