Permutations | INTUITIVE | Backtracking Template | Two Ways | Similar Problems | Leetcode 46

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

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

  • @Tanyyaa_13
    @Tanyyaa_13 ปีที่แล้ว +15

    Thank you so much sir. I was waiting for it and your videos help alot to me. Once again, Thank you very much 🙏

  • @ShivamKumar-xv5ch
    @ShivamKumar-xv5ch 11 หลายเดือนก่อน +30

    better than love babbar and i am not joking

    • @codestorywithMIK
      @codestorywithMIK  11 หลายเดือนก่อน +4

      🙏🙏🙏❤️❤️❤️

    • @llaniketsingh1415
      @llaniketsingh1415 5 หลายเดือนก่อน +1

      Damn true 🙌🏻

    • @gui-codes
      @gui-codes 4 หลายเดือนก่อน +1

      True bhai.

    • @sankalpsrivastava9491
      @sankalpsrivastava9491 20 วันที่ผ่านมา +2

      following love babbar was the biggest mistake of coding journey

  • @monikavaid5083
    @monikavaid5083 ปีที่แล้ว +9

    I am getting addicted to this channel 💘

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

      Means a lot to me. Thank you so much 🙏😇

  • @AbhishekGupta-if3rb
    @AbhishekGupta-if3rb ปีที่แล้ว +10

    Khaandani backtracking wala question ❤ ezz bro. Solve karliya fir b video dekhne aaya taki views kam na ho

  • @ArjunSaxena-wl3qs
    @ArjunSaxena-wl3qs 7 หลายเดือนก่อน +3

    He is the one, we all were looking for !

  • @puravkumar1251
    @puravkumar1251 4 หลายเดือนก่อน +1

    Literally confusion of some questions using for loop and some without for loop is removed.
    Every yt teacher just use it randomly and explain that solution
    Thanku so much so helpful.🙏🙏

  • @kashishjain3179
    @kashishjain3179 9 หลายเดือนก่อน +2

    Java Solution for the explanation -
    class Solution {
    List result = new ArrayList();
    public void backtrack(List temp ,int[] nums){
    int n = nums.length;
    if(temp.size()==n){
    result.add(new ArrayList(temp));
    return;
    }
    for(int i=0;i

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

    With each Video You are getting more and more better

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

    I will be forever thankful to this channel ❤

  • @s78_EditZ
    @s78_EditZ 4 หลายเดือนก่อน +1

    thank you so much sir from past four days i am learning from you now i feel much confident❤

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

    Even after solve a problem see your video to get in depth intuition for solve one problem and every time I able learn new thing bhaia thanks for your effort bhaia.

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

    thanks for the explanation. i also tried without solving the for-loop but reached the same conclusion and your video clarified that. i was able to solve this myself. i was wondering how can we handle the duplicates(like in permutations II).

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

      Yes thats the catch .
      I will soon make video on that too.
      (In short, sort the array and ensure that you don’t pick similar element twice)

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

      @@codestorywithMIK my first approach was removing duplicates from the input array i.e [1,1,2] => [1,2] but this did not work 😂
      so i tried the following approach which works buts its not considered good because we are changing i at two different places
      var permuteUnique = function (nums) {
      const results = [];
      nums.sort((a, b) => a - b);
      const solve = (temp, used) => {
      if (temp.length == nums.length) {
      results.push(temp.slice());
      return;
      }
      for (let i = 0; i < nums.length; i++) {
      if (used[i]) {
      continue;
      }
      temp.push(nums[i]);
      used[i] = true;
      solve(temp, used);
      temp.pop();
      used[i] = false;
      while (i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
      }
      };
      solve([], new Array(nums.length).fill(false));
      return results;
      };

  • @avi1882
    @avi1882 หลายเดือนก่อน +1

    What is the time complexity of 2nd Approach

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

    Again , I am back doing this August challenge.Thankyou for your daily video reminder.

  • @akashsingh-pm8pj
    @akashsingh-pm8pj ปีที่แล้ว +1

    Yesterday you gave this as an homework I solved it yesterday only....today I solved permutations ll .......your videos are literally awesome man...your teaching skills are bestest so far best jitne bhi dsa gurus bethe hai ......all the best bro...ps we are connected on LinkedIn 😆😆

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

      Thank you so much
      Means a lot ❤️❤️❤️

  • @NehaGoyal-s1q
    @NehaGoyal-s1q ปีที่แล้ว

    Please tell the time complexity of the second approach as well
    Also, I did not understand the time complexity of the first approach. Please explain

  • @udaykulkarni5639
    @udaykulkarni5639 8 หลายเดือนก่อน +1

    Earned a sub my man!! Keep up the good work!

  • @PRAVEENKUMAR-fi1wu
    @PRAVEENKUMAR-fi1wu 4 หลายเดือนก่อน

    Instead of changing approach, can we add only those subarrays whose length is equal to given array??

  • @ammumddhareppagol4898
    @ammumddhareppagol4898 3 หลายเดือนก่อน +1

    Bestest explanation ✨

  • @YashMalav-kh1ov
    @YashMalav-kh1ov 6 หลายเดือนก่อน +1

    ig the best explanation of this topic on youtube u are fire men 😇😅

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

    thank you sir,
    with the help of yours i will solve leetcode questions of August month. and i will get my 1st leetcode badge

  • @rockykumarverma980
    @rockykumarverma980 10 หลายเดือนก่อน

    Such a great explanation with so much stillness. Thank you so much sir 🥰🥰🥰

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

    Amazing explanation❤

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

    You are the best! I hope you will cover everything from easy to hard on this channel. I did this question earlier as it is very popular, submitted again to maintain a streak. Now I was going to sleep but was missing your video, so I came here to see the solution of the question I already know because I was feeling your way is unique and will definitely learn something new from this, and guess what? I was right.

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

      It means a lot to me.
      Thank you so much Varun 😇❤️

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

    Great Explanation 😍

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

    After Watching Your videos now i am getting confidence and I first tried to solve by myself and I stuck because their I was not using for loop , your video give me guidance on why I need to use for loop here, Thanks a Lot I am gaining my confidence, ❤ , Just One suggestion can you please start solving medium and hard Level POTD of GFG ,Please it will be very helpful that we will be able to learn 2 problem in a day in very deep manner , also 2 concepts someday.

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

      Means a lot
      Thank you so much
      Soon i will try to cover GFG POTD as well. Just having time crunch these days due to office also

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

      @@codestorywithMIK sure , waiting for that also ☺️

  • @oqant0424
    @oqant0424 3 หลายเดือนก่อน

    What will be the time complexity for swapping approach?

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

    why use set() when the result list temp itself has contains() method : set() can find in O(1) vs list can find in O(n) time. But using set creates O(n) extra space. "Space-Time tradeoff "
    but since problem statement specifies that 1

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

      Thanks a lot for this.
      It will definitely help others ❤️❤️

  • @kishan.17
    @kishan.17 ปีที่แล้ว +1

    super explanation bro.really you are a great storty teller

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

    Really great explanation everyone can easily understand 👍 👏 keep it up. Thank you 😊

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

    Long break Start watching again from today sir please upload daily leetcode

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

    Bitmask wala approach bhi samjadete bhaiya, btw loved your explanation

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

    Sir please make a video on word break, word break 2 With DP.

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

    Permutations 2 ka bhi video na and discuss all possible approach pls 🙏

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

    Thank you so much for this explanation ❤

  • @lagbhagadarsh-x6x
    @lagbhagadarsh-x6x ปีที่แล้ว +1

    Hi Mik your intution building is great...can you please please create a playlist for recurrsion...it will be really helpful

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

      Thank you so much.
      Sure thing.
      I have been planning this very soon.
      However, got really occupied due to office , travel etc.
      But very soon. Promise

    • @AmanYadav-jf1yy
      @AmanYadav-jf1yy ปีที่แล้ว +1

      @@codestorywithMIK Please bhaiya bana dijiye🥰🥰🥰🥰

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

    Make videos on Subset-II and combination sum 1

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

    Super explanation bhai! Bro can we have chapter-wise break up in the video, so that we could jump back and forth into a particular section easily like :
    Chapter 1 : Problem intro
    Chapter 2 : Intuition and explanation
    Chapter 3 : Dry run
    Chapter 4 : Code
    Chapter 5 : Time complexity explanation.

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

      Thank you so much.
      I have added the time stamps / chapters. You can now access particular sections. 😇🙏

  • @aritrakar2492
    @aritrakar2492 4 หลายเดือนก่อน

    thanks a lot sir , samaj aa gaya puraa

  • @ramankr0022
    @ramankr0022 7 หลายเดือนก่อน +1

    #awesome

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

    class Solution {
    public:
    vector ans;
    void solve(unordered_set st,vector &nums){
    if(st.size()==nums.size()){
    vector temp(st.begin(),st.end());
    ans.push_back(temp);
    return;
    }

    for(int i=0;i

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

      Can you share what error you are getting ?

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

      @@codestorywithMIK It is giving wrong output for [6,2,-1,8]

  • @shabazalam2374
    @shabazalam2374 6 หลายเดือนก่อน +1

    I watch , learn and write code in java.

  • @Lakshya-f4l
    @Lakshya-f4l 3 หลายเดือนก่อน +1

    Mark for revision :-)

  • @vinayjoshi8714
    @vinayjoshi8714 6 หลายเดือนก่อน

    Amazing explanation ❤❤

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

    One question , how to differentiate that this is recursion problem and backtracking problem??

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

      Backtracking always asks for Total possibilities, combinations etc
      And DP usually asks to optimise the result (minimum cost)

  • @kartikkaushik4743
    @kartikkaushik4743 หลายเดือนก่อน +1

    You are awesome 💯 mik ,I saw u started potd after a duration of long break pls continue it u are doing good for us 🙏

    • @codestorywithMIK
      @codestorywithMIK  หลายเดือนก่อน +2

      Yes, now I upload everyday. ♥️🙏

  • @satyamkumaryadav1560
    @satyamkumaryadav1560 ปีที่แล้ว +5

    @everyone im giving you the link of code help love babbar solution for this question match the content this person has solved the doubts that can come and babbar just gave the solution you decide you want to learn and grow or just learn
    no hates and offense just my p.o.v

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

      Totally agree 💯

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

      Agree 🔥

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

      Yep correct. People will soon realise how different this channel is

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

      Correct ✅

    • @AlishaKhan-ww3io
      @AlishaKhan-ww3io ปีที่แล้ว

      This is a fact. people need to see this.
      This comment should be pinned.

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

    Lol, U asked to solve it yesterday, so i did. Thus I was able to solve it from my bathroom itself (copy+paste😂)

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

    Longest repeating and non-overlapping substring
    DP problem,
    can anyone solve this ques with recursion +memo?

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

    Method 1:simple swapping
    class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
    def helper(index,nums,l,ans):
    if index==len(nums):
    ans.append(nums[:])
    start=index
    for j in range(start,len(nums)):
    nums[j],nums[index]=nums[index],nums[j]
    helper(index+1,nums,l,ans)
    nums[index],nums[j]=nums[j],nums[index]
    l=[]
    ans=[]
    helper(0,nums,l,ans)
    return ans
    Method 2:using for loop
    class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
    def helper(nums,l,ans,map):
    if len(l)==len(nums):
    ans.append(l[:])
    for j in range(len(nums)):
    if j not in map:
    map.add(j)
    l.append(nums[j])
    helper(nums,l,ans,map)
    l.pop()
    map.remove(j)
    l=[]
    ans=[]
    map=set()
    helper(nums,l,ans,map)
    return ans

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

    Good morning MIk 🌅 POTD ✅

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

    Solving skill is better going top thank

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

    subsets question is not solving using for loop method can anyone help

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

    Why we do undo ??? Pls explain it

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

      Because we have to explore every possibility.
      Exploring different possibilities can only be done by undoing

  • @abhisheksah1867
    @abhisheksah1867 4 หลายเดือนก่อน +1

    thanks sir

  • @ArjunSaxena-wl3qs
    @ArjunSaxena-wl3qs 7 หลายเดือนก่อน +1

    I just made up a story in my mind for the question 47
    like accha here i have to do like pura toh dekho, but repeated hai numbers toh na kyu na ek frequency counter lekar chaloon
    class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:


    res = set()
    n = len(nums)
    hm = Counter(nums)
    de = defaultdict(int)
    def bt(cur,de) :
    if len(cur) == n :
    res.add(tuple(cur[:]))

    for i in range(n) :
    if de[nums[i]] < hm[nums[i]]:
    de[nums[i]] += 1
    cur.append(nums[i])
    bt(cur,de)
    de[nums[i]] -= 1
    cur.pop()

    bt([],de)
    return res

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

    sir muze backtrack ki ek bat samjhi nahi me recursion to samjh gaya backtrack bhi samjh gaya par backtrack use kyu kiya vo nahi samajha hu ise only recursion and dp se solve kar sakte the kya

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

    Recursion ke andar for loop samjh nahi aata bhaiya kya karun ? :( ;

  • @invinciblearmyvlogsshorts7911
    @invinciblearmyvlogsshorts7911 11 หลายเดือนก่อน

    but bhaiya if i put i+1 instead of idx+1 it will be same thing?

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

    Thanku bhaiya ❤

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

    My man!!

  • @abhinay.k
    @abhinay.k หลายเดือนก่อน

    thanks

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

    Sir why did'nt we use unordered_map instead of set?

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

      void solve(vector& nums, vector&ans, vector&op, unordered_map&mp){
      if(ans.size()==nums.size()){
      op.push_back(ans);
      return;
      }
      for(int i=0; i

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

      You can use both

  • @oqant0424
    @oqant0424 3 หลายเดือนก่อน

    done [4.7.24]✅✅

  • @ujjwalshrivastava-s6z
    @ujjwalshrivastava-s6z 5 หลายเดือนก่อน

    this is very hard sir...cant even understand never understand this recursion?

    • @codestorywithMIK
      @codestorywithMIK  5 หลายเดือนก่อน

      Please first get a good grasp on Recursion Concepts. It will definitely help.
      th-cam.com/play/PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM.html&si=_3NfOjvBJJbSF9GU

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

    ❤❤

  • @Virtualexist
    @Virtualexist 3 หลายเดือนก่อน +1

    Par fir ham letter combination of phone number mein i = 0 s kyun start kiya?

    • @Virtualexist
      @Virtualexist 2 หลายเดือนก่อน

      Ooh got it. Kyuki hamein doosre string ko bhi poora iterate karna tha 0 s hi.

    • @Virtualexist
      @Virtualexist 2 หลายเดือนก่อน

      Ismein idx s start isliye kia kyuki next wale ko swap karke idx s peechey fix kar dia or ab aagey k numbers ki swapping karenge peechey walo ki nahi.

    • @Virtualexist
      @Virtualexist 2 หลายเดือนก่อน

      Now my question is next recursion call mein hamne i+1 s kyu nahi kia call?

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

    Sub set2(contains duplicate)
    Public listsubset with Dup(int[]nums);
    listlist=new Array list ();
    return list;
    private void backtrack (list >list >list,list temp list,nums,int start){
    list.add(newArray list (temp list);
    for(int i=start; istart &&num[i]==num[i-1]) continue
    templist.add[num(i)];
    backtrack (list,temp list,nums,i+1);
    temp list. remove (temp list ()-1)};
    }
    }
    Array .sort(nums);
    backtrack (list,new Array list(),nums,0)

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

    2/30 #augustchallengewithMIK

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

      Mentioned this hashtag in my today’s Leetcode POTD video.
      Thank you so much 😇🙏🙏

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

    WHY THIS WILL NOT WORK??
    class Solution {
    public:
    vector ans;
    void solve(unordered_set st,vector &nums){
    if(st.size()==nums.size()){
    vector temp(st.begin(),st.end());
    ans.push_back(temp);
    return;
    }

    for(int i=0;i

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

      Can you share what test failed ?

    • @DevanshAgarwal-tf7jt
      @DevanshAgarwal-tf7jt ปีที่แล้ว +1

      I tried doing the same thing (passing the set itself, instead of the vector and keeping set separate).
      This will not work because "unordered_set", as the name suggests, doesnt maintain insertion order. You will need to use a vector as insertion order is required for finding permutations.

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

      @@DevanshAgarwal-tf7jt Thank You So much.

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

      Sorry i missed to check after office
      Thanks for covering this 😇❤️

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

    push_back complexity is Amortized constant right ? then why we have take N ?

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

      We are pushing an entire vector (of size N) . While pushing entire copy is made for which I have considered O(N)

  • @gui-codes
    @gui-codes 4 หลายเดือนก่อน

    bawaal explanation.

  • @SunnyKumar-xc4fu
    @SunnyKumar-xc4fu 7 หลายเดือนก่อน

    java code : "class Solution {
    int n;
    List res=new ArrayList();
    public List permute(int[] nums) {
    n=nums.length;
    backtracker(0,nums);
    return res;
    }
    public void backtracker(int index,int[] nums)
    {
    if(n