Permutations | Leet code 46 | Theory explained + Python code
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
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!
way better explained and coded than any other video I could find, thank you
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!
Thanks a lot I'm glad it helps :)
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.
Thanks you I'm glad these vides are helpful!!
Thank you, this is the first explanation that actually made sense to me!
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
This is one of the simplest and most comprehensive solutions i've ever come across pertaining to this problem . Thank you
Your vids are so helpful that I find myself specifically picking mediums/hards that you've covered!
I'm glad the videos are helping!!
Great explanation!
Great Explanation🙏
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.
omg awesome bruh, you are the best since you are the only one treat us like *we know nothing* 😂
Perfect! Thanks, Sai.
Excellent clarity of communication
Glad it helped
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 🔥🔥
Happy to help and thank you!!
Thanks...That was so helpful
Thank you! Well explained.
amazing video 😭
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.
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
very nice solution ~ the other solutions i saw for permutations were bending my mind, but this one is super clear. thx
Great Explanation!!!!
Glad it was helpful!
thank you for such an amazing explanation!!!!
amazing crystal clear solution....thankyou for explanation
Wow just awesome!!! This channel is gonna blow up someday :)
Glad it helped!!
Can you please tell a solution using this approach only but not using slicing. How can this be implemented using Append and Pop
great solution!
Is it possible to generate all possible differences between a pair of numbers in a list: e.g., lst = [1, 2, 3, 4]?
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
Thank you for explaining the time and space complexity!
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)
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 ?
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?
How to get p permutations of n numbers? Like 5 permutations from 10 random numbers
Thanks!
Thanks for the explanation
glad it helped!
Soooo good
I love watching your video and please come on!
Thank you! I'm not too sure what you mean by the second part.
@@saianishmalla2646 oh sorry, I mean please keep on updating new videos!
haha yes ofc :)
Thx for the video, is the solution iterative or recursive?
Path=[1,2,3] then it reaches a base case and path will be append to the result list.What will be next step?
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.
@@saianishmalla2646 thank you😌
Your explanation is very clear and explicit!
Glad it was helpful!
I want support in python can you help me?
This solution as is doesn't work. Copy and paste it to see. Structure of result etc is broken
try this on n=10^6 lol