Skip to content

Commit da9900f

Browse files
committed
Create Daily 08-04-24.md
1 parent 48550ad commit da9900f

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

2024 April/Daily 08-04-24.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
## Today's 08-04-24 [Problem Link](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/description/?envType=daily-question&envId=2024-04-08)
2+
## 1700. Number of Students Unable to Eat Lunch
3+
4+
# Intuition
5+
<!-- Describe your first thoughts on how to solve this problem. -->
6+
The task is to count the number of students who will be unable to get a sandwich. There are two types of sandwiches, circle, and square, represented by the integers 0 and 1 respectively. Students are also represented by integers, where 0 indicates a preference for circle sandwiches and 1 indicates a preference for square sandwiches.
7+
8+
# Approach
9+
<!-- Describe your approach to solving the problem. -->
10+
11+
**Counting Students** :
12+
- I iterated through the `students` array and count the number of students preferring each type of sandwich.
13+
- Maintained separate counters for circle and square sandwiches (`circleStudentCount` and `squareStudentCount`).
14+
15+
**Serving Sandwiches** :
16+
- Iterated through the `sandwiches` array.
17+
- For each sandwich :
18+
- If it's a circle sandwich (`sandwich == 0`), check if there are remaining students preferring circle sandwiches (`circleStudentCount > 0`).
19+
- If yes, serve the sandwich to a circle student by decrementing `circleStudentCount`.
20+
- If no circle students left, return the count of remaining square students (`squareStudentCount`).
21+
- If it's a square sandwich (`sandwich == 1`), check if there are remaining students preferring square sandwiches (`squareStudentCount > 0`).
22+
- If yes, serve the sandwich to a square student by decrementing `squareStudentCount`.
23+
- If no square students left, returned the count of remaining circle students (`circleStudentCount`).
24+
25+
**Return** :
26+
- If all sandwiches are served successfully, returned 0, indicating no students are left without sandwiches.
27+
28+
---
29+
Have a look at the code , still have any confusion then please let me know in the comments
30+
Keep Solving.:)
31+
32+
# Complexity
33+
- Time complexity : $O(n + m)$
34+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
35+
$n$ : number of students
36+
$m$ : number of sandwiches
37+
- Space complexity : $O(1)$
38+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
39+
40+
# Code
41+
```
42+
class Solution {
43+
public int countStudents(int[] students, int[] sandwiches) {
44+
45+
// Count of students preferring circle sandwiches
46+
int circleStudentCount = 0;
47+
// Count of students preferring square sandwiches
48+
int squareStudentCount = 0;
49+
50+
// Counting the number of students preferring each type of sandwich
51+
for(int student : students) {
52+
if(student == 0) {
53+
circleStudentCount++;
54+
}
55+
else {
56+
squareStudentCount++;
57+
}
58+
}
59+
60+
// Iterating through sandwiches and serving them to students
61+
for(int sandwich : sandwiches) {
62+
63+
// If the sandwich type is circle and there are no more students preferring circle sandwiches, return the count of students preferring square sandwiches
64+
if(sandwich == 0 && circleStudentCount == 0) {
65+
return squareStudentCount;
66+
}
67+
// If the sandwich type is square and there are no more students preferring square sandwiches, return the count of students preferring circle sandwiches
68+
if(sandwich == 1 && squareStudentCount == 0) {
69+
return circleStudentCount;
70+
}
71+
// If the sandwich type is circle, serve it to a circle student and decrease the count of circle students
72+
if(sandwich == 0) {
73+
circleStudentCount--;
74+
}
75+
// If the sandwich type is square, serve it to a square student and decrease the count of square students
76+
else {
77+
squareStudentCount--;
78+
}
79+
}
80+
// All sandwiches have been served without any issues
81+
return 0;
82+
}
83+
}
84+
85+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy