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

  Рет қаралды 11,061

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/problems/number-...
0:00 - Read the problem
0:19 - Drawing Explanation
6:03 - Coding Explanation
leetcode 1700
#neetcode #leetcode #python

Пікірлер: 34
@loflog
@loflog 2 ай бұрын
With the inflation these days, no one’s eating lunch
@amongus-rq2hb
@amongus-rq2hb 2 ай бұрын
Finished on my own and came for the explanation. Thanks
@gabrieldavi9174
@gabrieldavi9174 Ай бұрын
I was really overthinking this problem.
@doc9448
@doc9448 Ай бұрын
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 8 күн бұрын
True @neetCode
@bohdanzaichenko
@bohdanzaichenko 15 сағат бұрын
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 44 минут бұрын
@@bohdanzaichenko you can solve it using queue
@xavier7769
@xavier7769 2 ай бұрын
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
@Timjstewart
@Timjstewart 3 күн бұрын
Nice solution to what is, sadly and literally, a first world problem.
@JoshPeterson
@JoshPeterson Ай бұрын
This is in the DSA course, but the video isn't linked
@satyamjha68
@satyamjha68 2 ай бұрын
Solved it!
@krateskim4169
@krateskim4169 2 ай бұрын
Awesome explanation
@varunpalsingh3822
@varunpalsingh3822 2 ай бұрын
thank you neet :-)
@licokr
@licokr 2 ай бұрын
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 Ай бұрын
how you did with the queue? how to handle the deadlock situation?
@get_out_it
@get_out_it 2 ай бұрын
It’s a great solution
@spsc07
@spsc07 2 ай бұрын
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 2 ай бұрын
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)
@servantofthelord8147
@servantofthelord8147 18 сағат бұрын
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)
@rithikgandhi3685
@rithikgandhi3685 2 ай бұрын
Wow. I so overengineered this problem
@1vader
@1vader Ай бұрын
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.
@staywithmeforever
@staywithmeforever 2 ай бұрын
we could do like return cnt[0]+cnt[1]
@DBagg-zz4ip
@DBagg-zz4ip Ай бұрын
oh I just did this one but messed around popping from the student list
@s8x.
@s8x. 2 ай бұрын
neet how did u learn everything? what’s resources did u use?
@adama7752
@adama7752 2 ай бұрын
Life lessons and hashmaps
@s8x.
@s8x. 2 ай бұрын
@@adama7752 neet
@spsc07
@spsc07 2 ай бұрын
@@adama7752 lmao
@sohampatil5670
@sohampatil5670 2 ай бұрын
@@adama7752 bruh
@seanmorris6185
@seanmorris6185 2 ай бұрын
What drawing pad do you use for writing your thoughts out on screen?
@infoknow3278
@infoknow3278 2 ай бұрын
3d paint
@neks2081
@neks2081 2 ай бұрын
mouse
@jenwans3055
@jenwans3055 2 ай бұрын
Do you have simple problems and sol. for blokes like me?
@adityamwagh
@adityamwagh 2 ай бұрын
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.
@miaowcat1
@miaowcat1 Ай бұрын
I'm just here for the comment section
Time Needed to Buy Tickets - Leetcode 2073 - Python
12:45
NeetCodeIO
Рет қаралды 12 М.
Be kind🤝
00:22
ISSEI / いっせい
Рет қаралды 22 МЛН
Why You Should Always Help Others ❤️
00:40
Alan Chikin Chow
Рет қаралды 27 МЛН
Sigma Girl Education #sigma #viral #comedy
00:16
CRAZY GREAPA
Рет қаралды 100 МЛН
When someone reclines their seat ✈️
00:21
Adam W
Рет қаралды 19 МЛН
Number of Recent Calls | Leet code 933 | Theory explained + Python code
7:33
Path Crossing - Leetcode 1496 - Python
7:31
NeetCodeIO
Рет қаралды 12 М.
Searching Strings [Pt 6] | C# for Beginners
15:50
dotnet
Рет қаралды 20 М.
lofi hip hop radio 📚 - beats to relax/study to
Lofi Girl
Рет қаралды 29 М.
1700. Number of Students Unable to Eat Lunch (Leetcode Easy)
5:23
Programming Live with Larry
Рет қаралды 6 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 168 М.
Be kind🤝
00:22
ISSEI / いっせい
Рет қаралды 22 МЛН