Number of Music Playlists - Leetcode 920 - Python

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

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

  • @YeetusOfHouseFetus
    @YeetusOfHouseFetus ปีที่แล้ว +18

    Return of the king 🙇‍♂️

  • @Cheng-K
    @Cheng-K ปีที่แล้ว +14

    Happy to see that you are back at leetcoding 💪

    • @sprajosh
      @sprajosh 7 หลายเดือนก่อน

      -leetcoding- neetcoding 😆

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

    Unique makes more sense than old_songs

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

      Yeah that's fair. Old makes more sense to me since the two decisions we're making are old vs new songs, but its a little confusing either way.
      Better than i, j tho like most solutions on lc

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

      ​@@NeetCodeIOthat's kind of clean code!

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

    Missed your daily videos so much. Glad you're back 🥳

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

    dontr stop uploading videos, you are the best

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

    I am thinking why did you stopped making videos in Neetcode ............ Now I got . Just now solving that problem and searched for the solution , then i have seen your new channel

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

    At 13:16 when you said I didn't explain "entirely". I'm like, bruh... you explain everything.

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

    Go the Mr Beast route..
    Try doing leetcode under crazy scenarios, for e.g:
    1. you can only use a mobile
    2. you can type but you have to look at the screen from the mirror
    3. you can press backspace only after each submission
    4. code underwater etc.
    Go crazy, get more views will make you feel better for leaving your google job XD ( just kidding... you are the GOAT!)

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

    You're back!!

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

    In second base case
    Why we using old_songs > n rather than old_songs < n??

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

    Thanks for the daily

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

    Studying computer science would be much more difficult without you

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

    Is it fine , if I was unable to solve this question after thinking a lot on my first try ?

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

    Thanks for the providing the answer for daily

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

    honest question is it possible to solve a problem like this without seeing the solution prior? were u able to solve this without seeing the solution just based on your intuition?

  • @user-jk4gi3gi8k
    @user-jk4gi3gi8k ปีที่แล้ว

    Thanks for this better explanation ❤

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

    welcome back

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

    Thank you. It helped a lot

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

    Since we are working with the math in this solution, can you make a version of this solution that would generate all the playlist combinations? Or could you link a solution for a simillar problem?
    Im having a tought time accepting/ understanding the reason behind "no. of new songs that we have" in :
    res = (n-old_songs) * count(curr_goal-1 , old_songs+1)

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

      think of it this way, n is the total of unique songs, old_song is the unique songs we have used so far, so theoretically, we have (n - old_song) of possible new/unused songs available to us.

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

    i am still somewhat confused. is there a easier version of similar problem? also can anyone explain this line.
    res += (used_songs - k) * count(target - 1, used_songs)
    will this properly calculate combinations when goal is much bigger than n? in my head after getting playlist of length 2n it will not care about keeping k gaps.

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

    when will old_songs ever be > n?

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

    Yesterday, I made the same wrong assumption about K songs. Eventually gave up on the problem.....

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

    well, it seems like a middle school math problem with few extra steps. A few more.

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

    Day 2 Streak Maintained !!

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

      :D can you share on how did you manage to do that?!

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

      @@ngneerin 🥲🥲🥲🥲

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

    Impressive work :)

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

    great video thanks

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

    great explanation

  • @dhanesh.s
    @dhanesh.s ปีที่แล้ว

    Please drop more on Beginners's System Design on your Course🙏

  • @atharvakulkarni2180
    @atharvakulkarni2180 8 หลายเดือนก่อน

    In the first example, why can't [1,2,1] be the answer? since difference between last 1 and first 1 is k

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

      Each playlist should have all songs from n mentioned atleast once. Since there is no 3 mentioned atleast once. it is not correct

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

    Can anyone post the tabulation solution? having a bit of trouble with it.

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

    I love you neetcode

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

    Veri intutive bro... :)

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

    This problem is a monster

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

    Adding a Pair as Key in Hashmap is just not good, then you have to update its equals and hashCode

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

    nice!

  • @MiguelLopez-ph4qb
    @MiguelLopez-ph4qb ปีที่แล้ว

    Nuts

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

    Password yaad aa gya achanak se?

  • @AftabAlam-gh1nh
    @AftabAlam-gh1nh ปีที่แล้ว

    Still not understood

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

  • @aditya-lr2in
    @aditya-lr2in ปีที่แล้ว +1

    grab ur under 1k views ticket here

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

    Java recursion - Time limit exceeded solution:
    class Solution {
    int mod = 1_000_000_000 + 7;
    public int numMusicPlaylists(int n, int goal, int k) {
    return (int)count(goal, 0, n, k);
    }
    private long count(int curGoal, int oldSongs, int n, int k) {
    if(curGoal == 0 && oldSongs == n) //curGoal reached with exactly n songs.
    return 1;
    if(curGoal == 0 || oldSongs > n) //1. curGoal is reached but oldSongs !=n, 2. curGoal reached with oldsongs > n
    return 0;
    long picknew = 0;
    //choose new song
    picknew = (n - oldSongs ) * count(curGoal - 1, oldSongs + 1, n, k) ; // new songs = n - oldsongs. and new goal will be curGoal -1, and since we picked new song, it is now added to old songs, so old songs + 1

    long pickOld = 0;
    //choose old song
    if(oldSongs > k)
    pickOld = (oldSongs - k ) * count(curGoal - 1, oldSongs, n, k) ; //we can pick oldsongs only if difference is k, curGoal decreases by 1 and since we did not pick new song, old songs count remains same.

    return (picknew % mod + pickOld % mod) % mod;
    }
    }

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

    Java memoization solution:
    class Solution {
    int mod = 1_000_000_000 + 7;
    long[][] memo;
    public int numMusicPlaylists(int n, int goal, int k) {
    memo = new long [goal + 1][n + 1]; //goal, oldSongs array.
    for(long[] arr: memo) {
    Arrays.fill(arr, -1);
    }
    return (int)count(goal, 0, n, k);
    }
    private long count(int curGoal, int oldSongs, int n, int k) {
    if(curGoal == 0 && oldSongs == n) //curGoal reached with exactly n songs.
    return 1;
    if(curGoal == 0 || oldSongs > n) //1. curGoal is reached but oldSongs !=n, 2. curGoal reached with oldsongs > n
    return 0;
    if(memo[curGoal][oldSongs] != -1)
    return memo[curGoal][oldSongs];
    long picknew = 0;
    //choose new song
    picknew = (n - oldSongs ) * count(curGoal - 1, oldSongs + 1, n, k) ; // new songs = n - oldsongs. and new goal will be curGoal -1, and since we picked new song, it is now added to old songs, so old songs + 1

    long pickOld = 0;
    //choose old song
    if(oldSongs > k)
    pickOld = (oldSongs - k ) * count(curGoal - 1, oldSongs, n, k) ; //we can pick oldsongs only if difference is k, curGoal decreases by 1 and since we did not pick new song, old songs count remains same.

    memo[curGoal][oldSongs] = (picknew % mod + pickOld % mod) % mod;

    return memo[curGoal][oldSongs];
    }
    }