Arrays | Leetcode 659 | Split Array Into Consecutive Subsequences

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • Topic: Arrays
    Time Complexity: O(n)
    Code:
    github.com/Nideesh1/Algo/blob...
    Leetcode:
    leetcode.com/problems/split-a...
    Note I claim no rights to this question. All rights belong to Leetcode. If I'm reviewing a solution that was from another Leetcode user or Leetcode itself I will give credit below.
    Credit to : Leetcode user compton_scatter | leetcode.com/problems/split-a...
    Hey there! Just wanted to let you know that some of the below links in this video description are affiliate links, which means that if you make a purchase through them, I may earn a small commission. Don't worry though, it doesn't cost you anything extra and it helps support this channel so I can continue to make more videos for you. Thank you so much for your support, and as always, all opinions are my own!
    Start getting great at system design: bytebytego.com?fpr=nideesh (affiliate link)
    Handpicked Algorithms and Data Structures for Interview To Save Time: interviewpen.com/?via=nideesh (affiliate link)
    Fast track to becoming a knowledgeable SWE www.educative.io/unlimited?af... (affiliate link)

Komentáře • 98

  • @Gurpreetkaur-ws4fv
    @Gurpreetkaur-ws4fv Před 3 lety +18

    This is the first video among all, which could clearly explain to me the logic of the greedy approach used here and get convinced with the intuition.
    1. Though i could not find any resources on the internet which could help me understand the process - How can I come up with such intuition.
    2. But at least, this video was really helpful to get convinced for the algorithm. and then I was able to write a correct code my self.
    Many Thanks.

  • @c9bd415
    @c9bd415 Před 4 lety +23

    Thank you, couldn't understand those descriptions on leetcode..

  • @bakguff
    @bakguff Před rokem +5

    Dude, great explanation. I was really confused by other explanations of this problem but this helped out alot!

  • @nagalakshmi1385
    @nagalakshmi1385 Před 4 lety +1

    great video as always! The best way to explain the intuition behind solving the problem, you always do best among all. Thanks a lot !

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

    Seriously, nice job
    I saw a video of you some time ago, it's cool to see your evolution, and see you making them cleaner and better every time 👌

    • @NideeshTerapalli
      @NideeshTerapalli  Před 3 lety

      Thanks Thomas, I'm always getting a tiny bit better every day

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

    great explanation! very clear and easy to understand! I'm Chinese so I usually watch videos explained in Chinese bc it's easy to understand, but I found yours is much clearer! Thank you

  • @shenth27
    @shenth27 Před 4 lety

    Excellent explanation with always sticking to the point.

  • @sparshsharma6068
    @sparshsharma6068 Před 2 lety

    Halfway through the video and I got an AC on leetcode just by understanding the way you explained the intuition! Thanks a lot

  • @swapanshridhar1
    @swapanshridhar1 Před 3 lety

    Perfect. Very helpful explanation. Thanks!

  • @sachinwathore3773
    @sachinwathore3773 Před 4 lety

    Thanks a lot Nideesh for an awesome explanation. I found it very helpful !

  • @harshtyagi700
    @harshtyagi700 Před rokem

    You nailed the explaination part. Thanks!!

  • @rakshith3547
    @rakshith3547 Před 3 lety

    by far the best explanation for this problem.. keep up the good work bro

  • @tanmaypahuja206
    @tanmaypahuja206 Před 4 lety

    Amazing explanation! Thanks a lot!

  • @vishweshpujari2584
    @vishweshpujari2584 Před rokem

    Best explanation for this problem. Thanks!

  • @thegt
    @thegt Před 4 lety

    Very nice explanation! Great job man!

  • @manasdeora4601
    @manasdeora4601 Před rokem

    Loved the explanation. Could not think of this approach.

  • @nopecharon
    @nopecharon Před rokem

    What a beast explanation. You earned a new sub

  • @rajarshisarkar5488
    @rajarshisarkar5488 Před 4 lety

    Very clear explanation! Thanks.

  • @BackToBackSWE
    @BackToBackSWE Před 4 lety +14

    Do you have a contact? I want to talk to you

  • @toekneema
    @toekneema Před 3 lety

    thank you! great walkthrough!

  • @prempeacefulchannel
    @prempeacefulchannel Před rokem

    You explained it very clearly. Thanks a lot bro!

  • @chuyi8664
    @chuyi8664 Před 4 lety

    Thank you! it is really helpful

  • @prabhatkumar-eg2rg
    @prabhatkumar-eg2rg Před 3 lety +1

    Good stuff Nideesh. Keep cracking on.

  • @anderswb
    @anderswb Před 3 lety

    This solution is tricky to get your head around at first but this is a great and concise explanation.

  • @navidr2811
    @navidr2811 Před 4 lety

    great job explaining it man!

  • @quirkyquester
    @quirkyquester Před 2 lety

    this is a fire video brother! Thank you! brilliant!

  • @arthamsreenivas8858
    @arthamsreenivas8858 Před 4 lety +3

    Thank you brother, leetcode solution is very hard to understand, keep going on!

  • @AYJ959
    @AYJ959 Před rokem

    Thanks a lot dude , you explained it very well!

  • @ramanahlawat398
    @ramanahlawat398 Před rokem +1

    thanks man!! appreciate the help nice explanation too!!

  • @kewtomrao
    @kewtomrao Před rokem

    Thats an amazing explanation!

  • @rajatbudania6181
    @rajatbudania6181 Před 4 lety

    Plz upload more videos, these are really helpful mate!

  • @Cloud-577
    @Cloud-577 Před 2 lety

    thank you :) you are great at explaining

  • @fahadabdullah687
    @fahadabdullah687 Před rokem

    Greatly explained. It's todays daily challenge on leetcode. 19 aug 2022.

  • @mashab9129
    @mashab9129 Před 3 lety

    brilliant solution. thanks for sharing.

  • @diaoyuanhuzhenhuiqi
    @diaoyuanhuzhenhuiqi Před rokem

    really the best explaination i seen

  • @shantanukshire79
    @shantanukshire79 Před 4 lety

    Super solution !

  • @guolongli6667
    @guolongli6667 Před 4 lety +1

    NICE IDEA AND WONDERFUL EXPLANATION! you help me solve the problem :>

  • @learnninspire7984
    @learnninspire7984 Před rokem +1

    great explanation bro!

  • @kollivenkatamadhukar5059
    @kollivenkatamadhukar5059 Před 4 lety +1

    Can you please explain your taught process how you arrived at the solution, I mean how did you get to know you have to use an hypothetical map....

  • @sumitkumar-ps9rl
    @sumitkumar-ps9rl Před 4 lety

    HI Nideesh,
    It was really helpful. I used multiset from c++ in place of a map.
    Thank You.

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

    Superb Nideesh!

  • @ChanChan-pg4wu
    @ChanChan-pg4wu Před 2 lety

    Nice Work!

  • @bhargavamarpina8294
    @bhargavamarpina8294 Před 4 lety +1

    Nice Explanation.

  • @manojborugadda9212
    @manojborugadda9212 Před rokem

    Excellent explanation Nideesh anna😀😀🙌

  • @_ADITIKAJALE
    @_ADITIKAJALE Před 2 lety

    best explanation ! thanks :))

  • @kaichenghu3826
    @kaichenghu3826 Před 4 lety

    Thanks for your explanation

  • @pukhrajsoni4020
    @pukhrajsoni4020 Před 2 lety

    Excellent explanation

  • @saicharan8675
    @saicharan8675 Před 4 lety

    Super cool thanks for sharing

  • @yashbansal1520
    @yashbansal1520 Před 3 lety

    Great explanation

  • @iriswei3018
    @iriswei3018 Před 4 lety +1

    very helpful!

  • @rajatbudania6181
    @rajatbudania6181 Před 4 lety

    Very Nice Explanation! )-

  • @akhilchakravarthy
    @akhilchakravarthy Před 3 lety

    Good job dude

  • @NideeshTerapalli
    @NideeshTerapalli  Před rokem

    Hey there! Just wanted to let you know that some of the links in this comment are affiliate links, which means that if you make a purchase through them, I may earn a small commission. Don't worry though, it doesn't cost you anything extra and it helps support this channel so I can continue to make more videos for you. Thank you so much for your support, and as always, all opinions are my own!
    Start getting great at system design: bytebytego.com?fpr=nideesh (affiliate link)
    Handpicked Algorithms and Data Structures for Interview To Save Time: interviewpen.com/?via=nideesh (affiliate link)
    Fast track to becoming a knowledgeable SWE www.educative.io/unlimited?aff=K1z6 (affiliate link)

  • @nishantdhamija2880
    @nishantdhamija2880 Před rokem

    that was really good

  • @koushikkumar5156
    @koushikkumar5156 Před rokem

    Nice explanation

  • @RagazzoKZ
    @RagazzoKZ Před 4 lety

    Wow, new videos!

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety

      Yup, sorry for the delay. I remember you requested this one, hope it helps!

  • @nikhil.pandey
    @nikhil.pandey Před 2 lety

    smooth

  • @sabbispeak
    @sabbispeak Před 4 lety

    nice expaalanaation mate

  • @labi9500
    @labi9500 Před rokem

    thanks!

  • @ajourney179
    @ajourney179 Před 4 lety

    amazing.

  • @algorithmimplementer415
    @algorithmimplementer415 Před 4 lety +1

    Could you group this kind of questions that are a bit tricky and relevant to crack big tech company interview? Thanks!

  • @chamcham3806
    @chamcham3806 Před 4 lety

    Why are we trying to append to the hypothetical map first (preferring it over starting a new sequence from the current element) ?

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety +2

      Which line in the code are you referring to? Or you could tell me which part of the video you had in mind.
      But if you are asking in general why does an element try to append itself to an existing sequence instead of starting a new one, the reasons are as follows:
      1) This is a greedy approach
      2) If a number can join a subsequence that is already generated then it will do so because the algorithm's job is to identify (true/false) whether or not we can generate valid subsequences. If a number is always trying to start a new subsequence then it might not have all the elements in the array/frequency map to create a valid subsequence. That's why a number will prefer to join a subsequence if it can and in doing so create a new "hypothetical" spot for the next integer which may come from anywhere in the array.
      Feel free to ask more questions

    • @chamcham3806
      @chamcham3806 Před 4 lety

      @@NideeshTerapalli Thanks for your quick reply. Got it cleared.

  • @sarthakchoudhary811
    @sarthakchoudhary811 Před 4 lety

    very nice.

  • @akshatjain7742
    @akshatjain7742 Před 4 lety

    Why do we need to keep numbers with value 0 in the Hmap? Those are the numbers that can no longer be accepted, right?

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety +1

      Hmap is actually just hypothetical k-v storage. We might encounter a number later in the array which belong there. Feel free to ask more clarifying questions if i didn't explain clearly

  • @TrenBlack
    @TrenBlack Před 4 lety +1

    Have you ever done Candy Crush? It is leetcode 723... (You might need leetcode premium to access it)

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety

      Hey Tren, I won't be able to make videos on leetcode premium questions since they are private on their website. Sorry about that!

  • @jaesungj
    @jaesungj Před 4 lety +1

    While the explanation was really good do you have any good resource on the steps toward how we eventually came up with this algorithm?

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety +4

      Hi JJ, sorry I don't. I gave credit to the original post in the video description. Looks like "greedy" algos don't have the same templates as other questions like say backtracking. Will share it if I find though

  • @HOW--2
    @HOW--2 Před rokem

    What does hypothetical map mean bro ?

  • @gurpreetchawla2066
    @gurpreetchawla2066 Před 3 lety

    Hypothetical Map shows - ( key , demand of a number ) . Incase someone's confused what hypothetical map do.

  • @bismeetsingh352
    @bismeetsingh352 Před 3 lety

    Python solution: leetcode.com/problems/split-array-into-consecutive-subsequences/discuss/811017/Hypothetical-Map!!

  • @RagazzoKZ
    @RagazzoKZ Před 4 lety +2

    I think it would be interesting to consider edge cases. What if you have two sequences expecting the same number? In this case, you should push the number into the shorter sequence. I used a priority queue for that. But it's not the best solution.
    const map = new Map() // storing priority queues of sequences, key is the expecting number (i.e. next number in sequence)
    for (let num of nums) {
    let prevPq = map.get(num - 1)
    let subseq
    if (!prevPq) {
    subseq = new Subseq(num)
    } else {
    subseq = prevPq.pop()
    subseq.addLast(num)
    if (prevPq.isEmpty()) {
    map.delete(num - 1)
    }
    }
    let nextPq = map.get(num)
    if (!nextPq) {
    nextPq = new PriorityQueue()
    }
    nextPq.push(subseq)
    map.set(num, nextPq)
    }
    for (let [key, pq] of map) {
    for (let subseq of pq._heap) {
    if (!subseq.isValid()) {
    return false
    }
    }
    }
    return true
    my Subseq class:
    class Subseq {
    constructor(last) {
    this.last = last
    this.len = 1
    }
    addLast(last) {
    this.last = last
    this.len++
    }
    getLast() {
    return this.last
    }
    getLen() {
    return this.len
    }
    isValid() {
    return this.len > 2
    }
    }

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety +1

      Interesting, thanks for sharing this. I actually did not explore a solution with a Priority Queue, appreciate going over it.

  • @DSUNDAR_
    @DSUNDAR_ Před 21 dnem

    In short,
    3 cases :
    case1: can join existing group-> check anybody wants the current number by looking hypothetical map.
    case2: eligible to create a group
    case3: not eligible to create group as well cannot join any group. return false

  • @iriswei3018
    @iriswei3018 Před 4 lety +2

    btw, you are cute!

  • @karun4663
    @karun4663 Před 4 lety

    plz make oop videos

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety

      Hi Karun, yeah sure. I'm focusing on algos at the moment, then sys design. Eventually I will cover oop too

  • @cutsandstonebetablocksumo9891

    5:28 does anyone want me?

  • @AbhijeetNayak-connect
    @AbhijeetNayak-connect Před 3 lety

    Bad description of the problem, along with a +ve case, also explain a -ve input

  • @shubham28921
    @shubham28921 Před 4 lety

    1.25x

  • @BackToBackSWE
    @BackToBackSWE Před 4 lety +3

    Do you have a contact? I want to talk to you

    • @NideeshTerapalli
      @NideeshTerapalli  Před 4 lety

      Back To Back SWE hi you can email me at nterapalli1 @ [gmail . com ]

  • @BackToBackSWE
    @BackToBackSWE Před 4 lety +9

    Do you have a contact? I want to talk to you