Arrays | Leetcode 659 | Split Array Into Consecutive Subsequences
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)
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.
Thank you, couldn't understand those descriptions on leetcode..
Haha you're welcome
Dude, great explanation. I was really confused by other explanations of this problem but this helped out alot!
great video as always! The best way to explain the intuition behind solving the problem, you always do best among all. Thanks a lot !
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 👌
Thanks Thomas, I'm always getting a tiny bit better every day
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
Excellent explanation with always sticking to the point.
Halfway through the video and I got an AC on leetcode just by understanding the way you explained the intuition! Thanks a lot
Perfect. Very helpful explanation. Thanks!
Thanks a lot Nideesh for an awesome explanation. I found it very helpful !
You nailed the explaination part. Thanks!!
by far the best explanation for this problem.. keep up the good work bro
Amazing explanation! Thanks a lot!
Best explanation for this problem. Thanks!
Very nice explanation! Great job man!
Loved the explanation. Could not think of this approach.
What a beast explanation. You earned a new sub
Very clear explanation! Thanks.
Do you have a contact? I want to talk to you
thank you! great walkthrough!
You explained it very clearly. Thanks a lot bro!
Thank you! it is really helpful
Good stuff Nideesh. Keep cracking on.
Thanks Prabhat
This solution is tricky to get your head around at first but this is a great and concise explanation.
great job explaining it man!
this is a fire video brother! Thank you! brilliant!
Thank you brother, leetcode solution is very hard to understand, keep going on!
Thanks Artham
Thanks a lot dude , you explained it very well!
thanks man!! appreciate the help nice explanation too!!
Thats an amazing explanation!
Plz upload more videos, these are really helpful mate!
thank you :) you are great at explaining
Greatly explained. It's todays daily challenge on leetcode. 19 aug 2022.
brilliant solution. thanks for sharing.
really the best explaination i seen
Super solution !
NICE IDEA AND WONDERFUL EXPLANATION! you help me solve the problem :>
great explanation bro!
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....
HI Nideesh,
It was really helpful. I used multiset from c++ in place of a map.
Thank You.
Happy to help
Superb Nideesh!
Nice Work!
Nice Explanation.
Excellent explanation Nideesh anna😀😀🙌
best explanation ! thanks :))
Thanks for your explanation
You're welcome
Excellent explanation
Super cool thanks for sharing
Great explanation
very helpful!
You're welcome
Very Nice Explanation! )-
Good job dude
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)
that was really good
Nice explanation
Wow, new videos!
Yup, sorry for the delay. I remember you requested this one, hope it helps!
smooth
nice expaalanaation mate
thanks!
amazing.
Could you group this kind of questions that are a bit tricky and relevant to crack big tech company interview? Thanks!
Good idea
Why are we trying to append to the hypothetical map first (preferring it over starting a new sequence from the current element) ?
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
@@NideeshTerapalli Thanks for your quick reply. Got it cleared.
very nice.
Why do we need to keep numbers with value 0 in the Hmap? Those are the numbers that can no longer be accepted, right?
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
Have you ever done Candy Crush? It is leetcode 723... (You might need leetcode premium to access it)
Hey Tren, I won't be able to make videos on leetcode premium questions since they are private on their website. Sorry about that!
While the explanation was really good do you have any good resource on the steps toward how we eventually came up with this algorithm?
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
What does hypothetical map mean bro ?
Hypothetical Map shows - ( key , demand of a number ) . Incase someone's confused what hypothetical map do.
Python solution: leetcode.com/problems/split-array-into-consecutive-subsequences/discuss/811017/Hypothetical-Map!!
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
}
}
Interesting, thanks for sharing this. I actually did not explore a solution with a Priority Queue, appreciate going over it.
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
btw, you are cute!
Haha, i'm flattered thank you
plz make oop videos
Hi Karun, yeah sure. I'm focusing on algos at the moment, then sys design. Eventually I will cover oop too
5:28 does anyone want me?
Bad description of the problem, along with a +ve case, also explain a -ve input
1.25x
Do you have a contact? I want to talk to you
Back To Back SWE hi you can email me at nterapalli1 @ [gmail . com ]
Do you have a contact? I want to talk to you