Number of Students Unable to Eat Lunch - Leetcode 1700 - Python

  Рет қаралды 18,953

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 48
@loflog
@loflog 7 ай бұрын
With the inflation these days, no one’s eating lunch
@amongus-rq2hb
@amongus-rq2hb 7 ай бұрын
Finished on my own and came for the explanation. Thanks
@AhmedMarzookisabeast
@AhmedMarzookisabeast 2 ай бұрын
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
@souljarohill8795 Ай бұрын
same
@xavier7769
@xavier7769 7 ай бұрын
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
@gabrieldavi9174
@gabrieldavi9174 7 ай бұрын
I was really overthinking this problem.
@licokr
@licokr 7 ай бұрын
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.
@CodingResoures
@CodingResoures 7 ай бұрын
how you did with the queue? how to handle the deadlock situation?
@abdelrhmantarek3937
@abdelrhmantarek3937 Ай бұрын
@@CodingResoures class Solution { public: int countStudents(vector& students, vector& sandwiches) { std::queue StudentQueue; for(const auto& it : students) { StudentQueue.push(it); } int sandPointer = 0; int counter = 0; while(!StudentQueue.empty() && sandPointer != sandwiches.size()) { if(StudentQueue.front() == sandwiches[sandPointer]) { sandPointer++; StudentQueue.pop(); counter = 0; } else { counter++; if(counter == StudentQueue.size()) break; int rotatedValue = StudentQueue.front(); StudentQueue.pop(); StudentQueue.push(rotatedValue); } } return StudentQueue.size(); } };
@Shanoro
@Shanoro 2 ай бұрын
You have this video as a solution in your Pro course in the section Queues. This video does not solve anything with a queue.
@servantofthelord8147
@servantofthelord8147 5 ай бұрын
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)
@spsc07
@spsc07 7 ай бұрын
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; } };
@juanmacias5922
@juanmacias5922 7 ай бұрын
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)
@doc9448
@doc9448 7 ай бұрын
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.
@keyulpatel4181
@keyulpatel4181 6 ай бұрын
True @neetCode
@bohdanzaichenko
@bohdanzaichenko 5 ай бұрын
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
@brianvega9231
@brianvega9231 5 ай бұрын
@@bohdanzaichenko you can solve it using queue
@nehushtant
@nehushtant 5 ай бұрын
@@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
@GeekSlider
@GeekSlider 24 күн бұрын
@@bohdanzaichenko exactly werird
@JoshPeterson
@JoshPeterson 7 ай бұрын
This is in the DSA course, but the video isn't linked
@1vader
@1vader 7 ай бұрын
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.
@s8x. 7 ай бұрын
neet how did u learn everything? what’s resources did u use?
@adama7752
@adama7752 7 ай бұрын
Life lessons and hashmaps
@s8x.
@s8x. 7 ай бұрын
@@adama7752 neet
@spsc07
@spsc07 7 ай бұрын
@@adama7752 lmao
@sohampatil5670
@sohampatil5670 7 ай бұрын
@@adama7752 bruh
@Timjstewart
@Timjstewart 5 ай бұрын
Nice solution to what is, sadly and literally, a first world problem.
@rithikgandhi3685
@rithikgandhi3685 7 ай бұрын
Wow. I so overengineered this problem
@krateskim4169
@krateskim4169 7 ай бұрын
Awesome explanation
@satyamjha68
@satyamjha68 7 ай бұрын
Solved it!
@get_out_it
@get_out_it 7 ай бұрын
It’s a great solution
@jenwans3055
@jenwans3055 7 ай бұрын
Do you have simple problems and sol. for blokes like me?
@adityamwagh
@adityamwagh 7 ай бұрын
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.
@varunpalsingh3822
@varunpalsingh3822 7 ай бұрын
thank you neet :-)
@staywithmeforever
@staywithmeforever 7 ай бұрын
we could do like return cnt[0]+cnt[1]
@shepmax555
@shepmax555 2 ай бұрын
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-mr5tl
@ShreyBhardwaj-mr5tl 3 ай бұрын
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?
@seanmorris6185
@seanmorris6185 7 ай бұрын
What drawing pad do you use for writing your thoughts out on screen?
@infoknow3278
@infoknow3278 7 ай бұрын
3d paint
@neks2081
@neks2081 7 ай бұрын
mouse
@DBagg-zz4ip
@DBagg-zz4ip 7 ай бұрын
oh I just did this one but messed around popping from the student list
@lethnisoff
@lethnisoff 3 ай бұрын
man this is 300IQ solution
@innazhogova3621
@innazhogova3621 2 ай бұрын
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.
@goatking443
@goatking443 5 ай бұрын
you are so evil for including this as exercise in queue part of the course
Time Needed to Buy Tickets - Leetcode 2073 - Python
12:45
NeetCodeIO
Рет қаралды 14 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 151 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 22 МЛН
One day.. 🙌
00:33
Celine Dept
Рет қаралды 42 МЛН
Мама у нас строгая
00:20
VAVAN
Рет қаралды 11 МЛН
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 17 МЛН
Subarrays with K Different Integers - Leetcode 992 - Python
17:31
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 650 М.
Minimum Height Trees - Leetcode 310 - Python
23:30
NeetCodeIO
Рет қаралды 21 М.
Why you’re so tired
19:52
Johnny Harris
Рет қаралды 3,2 МЛН
Contiguous Array - Leetcode 525 - Python
15:41
NeetCodeIO
Рет қаралды 29 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 740 М.
Shortest Subarray with Sum at Least K - Leetcode 862 - Python
27:57
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 111 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 18 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 22 МЛН