With the inflation these days, no one’s eating lunch
@amongus-rq2hb7 ай бұрын
Finished on my own and came for the explanation. Thanks
@AhmedMarzookisabeast2 ай бұрын
Solution looks good I did it thinking of queues in mind and did a .pop() and .append() as I came from the Neetcode queue section seems a bit overengineered now. class Solution: def countStudents(self, students: list[int], sandwiches: list[int]) -> int: students_left = len(students) while students_left > 0: if students[0] == sandwiches[0] and students_left: students.pop(0) sandwiches.pop(0) students_left = len(students) else: student = students.pop(0) students.append(student) students_left -= 1 return len(students)
@souljarohill8795Ай бұрын
same
@xavier77697 ай бұрын
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: i = 0 # initialize pointer to first student while i < len(students) and len(students) != 0: # break if we have not found a student to pair with the first sandwich or all students are fed (no students left) if students[i] == sandwiches[0]: # if i-th student matches first sandwich students.pop(i) # removes the i-th student and first sandwich as a pair sandwiches.pop(0) i=0 # then resets pointer to first student else: i+=1 # otherwise if student sandwich pair not found, point to next student return len(students) # return students who have not eaten O(n^2) but still idk how to work with queues
@gabrieldavi91747 ай бұрын
I was really overthinking this problem.
@licokr7 ай бұрын
I initially approached with a dequeue (the same approach the problem tells) and the time complexity was O(n ^ 2). I thought there would be a better solution and there is. I focused on the question too much... Thank you so much👍 This is the code I wrote after watching your solution. I used cnt to calc the remaining students cnt = Counter(students) for s in sandwiches: if cnt[s] == 0: break cnt[s] -= 1 return cnt[0] + cnt[1] ps: either cnt[0] or cnt[1] is not zero if there are students unable to eat lunch.
@CodingResoures7 ай бұрын
how you did with the queue? how to handle the deadlock situation?
You have this video as a solution in your Pro course in the section Queues. This video does not solve anything with a queue.
@servantofthelord81475 ай бұрын
Any issue with just simulating the problem with deques here? : from collections import deque class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: sandwiches, students = deque(sandwiches),deque(students) skips = 0 while skips < len(students): if students[0] == sandwiches[0]: skips = 0 students.popleft() sandwiches.popleft() else: skips+=1 students.append(students.popleft()) return len(students)
@spsc077 ай бұрын
I did a simulation using deque in cpp class Solution { public: int countStudents(vector& st, vector& sd) { deque q(st.begin(),st.end()); int i=0,cnt=0; while(!q.empty()) { int curr=q.front(); q.pop_front(); if(curr!=sd[i]) { ++cnt; q.push_back(curr); } else { ++i; cnt=0; } if(cnt==q.size()) break; } return q.size(); } }; Edit1: Did your method as well class Solution { public: int countStudents(vector& st, vector& sd) { int ans=st.size(); vectorhashh(2); hashh[0]=count(st.begin(),st.end(),0); hashh[1]=ans-hashh[0]; for(int i:sd) { if(hashh[i]>0) { --ans; --hashh[i]; } else return ans; } return 0; } };
@juanmacias59227 ай бұрын
I was so close to get this problem using the Python Counter(), but didn't realize the order of the sandwich mattered lol so I also just did the simulation with deques using Python: def countStudents(students, sandwiches): students_deque = deque(students) sandwiches_deque = deque(sandwiches) i = 0 while (students_deque or sandwiches_deque) and i < len(students_deque): if students_deque[0] == sandwiches_deque[0]: students_deque.popleft() sandwiches_deque.popleft() i = 0 else: students_deque.append(students_deque.popleft()) i += 1 return len(students_deque)
@doc94487 ай бұрын
You need to add this to the DSA course now. There is no link in the Queue's section even though you have the video here (I arrived here by chance via google). (Edit: never mind. I see why you maybe didn't add this video. Because you didn't use a Queue, no?) Nice to see you're still doing leetcodes though. This is the first recent leetcode video I've seen. I was worried I'd eventually run out of content.
@keyulpatel41816 ай бұрын
True @neetCode
@bohdanzaichenko5 ай бұрын
I don't get it, he didn't use Queue here but it's still linked as a suggested problem for queue topic in his DSA beginner course
@brianvega92315 ай бұрын
@@bohdanzaichenko you can solve it using queue
@nehushtant5 ай бұрын
@@brianvega9231 Yeah but even then if he's gonna include it in the queue section, might as well write code for it. We are paying for this course after all
@GeekSlider24 күн бұрын
@@bohdanzaichenko exactly werird
@JoshPeterson7 ай бұрын
This is in the DSA course, but the video isn't linked
@1vader7 ай бұрын
You don't even need the "res" variable. You can just return "cnt[1 - s]" (and 0 if we reach outside the loop). In the else branch, cnt[s] is zero. cnt[1 - s] is the count of the other preference which is obviously all that can remain. And if we reach outside the loop, all the sandwiches were eaten.
@s8x.7 ай бұрын
neet how did u learn everything? what’s resources did u use?
@adama77527 ай бұрын
Life lessons and hashmaps
@s8x.7 ай бұрын
@@adama7752 neet
@spsc077 ай бұрын
@@adama7752 lmao
@sohampatil56707 ай бұрын
@@adama7752 bruh
@Timjstewart5 ай бұрын
Nice solution to what is, sadly and literally, a first world problem.
@rithikgandhi36857 ай бұрын
Wow. I so overengineered this problem
@krateskim41697 ай бұрын
Awesome explanation
@satyamjha687 ай бұрын
Solved it!
@get_out_it7 ай бұрын
It’s a great solution
@jenwans30557 ай бұрын
Do you have simple problems and sol. for blokes like me?
@adityamwagh7 ай бұрын
This is an easy tagged problem, but I agree it’s a bit on the harder end of easy questions. You can try some other easy questions on LeetCode or GeeksForGeeks.
@varunpalsingh38227 ай бұрын
thank you neet :-)
@staywithmeforever7 ай бұрын
we could do like return cnt[0]+cnt[1]
@shepmax5552 ай бұрын
this solution is acceptable, but not scalable in case you start offering more than 2 sandwiches. need to keep this in mind as well
@ShreyBhardwaj-mr5tl3 ай бұрын
How are you implementing the rule that if the student does not get what he wants i.e the top of the stack, then he moves to the end of the queue? Can anyone pls explain?
@seanmorris61857 ай бұрын
What drawing pad do you use for writing your thoughts out on screen?
@infoknow32787 ай бұрын
3d paint
@neks20817 ай бұрын
mouse
@DBagg-zz4ip7 ай бұрын
oh I just did this one but messed around popping from the student list
@lethnisoff3 ай бұрын
man this is 300IQ solution
@innazhogova36212 ай бұрын
The question makes no sense. Why is the video & the correct solution approaching both inputs like they're queues? Didn't it say that sandwiches is a stack? There's definitely a mistake in the description.
@goatking4435 ай бұрын
you are so evil for including this as exercise in queue part of the course