diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /num_student_unable_eat.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'num_student_unable_eat.c')
-rw-r--r-- | num_student_unable_eat.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/num_student_unable_eat.c b/num_student_unable_eat.c new file mode 100644 index 0000000..ebcd8fb --- /dev/null +++ b/num_student_unable_eat.c @@ -0,0 +1,41 @@ +// 1700. Number of Students Unable to Eat Lunch +#include "leetcode.h" + +/* + * the school cafeteria offers circular and square sandwiches at lunch break, + * referred to by numbers 0 and 1, respectively. all students stand in a queue. + * each student either prefers square or circular sandwiches. the number of + * sandwiches in the cafeteria is equal to the number of students. + */ + +int countStudents(int *students, int studentsSize, int *sandwiches, + int sandwichesSize) { + int n = studentsSize, one_cnt = 0, zero_cnt = 0; + for (int i = 0; i < n; i++) + if (students[i]) + one_cnt++; + zero_cnt = n - one_cnt; + for (int i = 0; i < n; i++) { + if (sandwiches[i]) { + if (one_cnt) + one_cnt--; + else + return n - i; + } else { + if (zero_cnt) + zero_cnt--; + else + return n - i; + } + } + return 0; +} + +int main() { + int st1[] = {1, 1, 0, 0}, sa1[] = {0, 1, 0, 1}, st2[] = {1, 1, 1, 0, 0, 1}, + sa2[] = {1, 0, 0, 0, 1, 1}; + printf("%d\n", countStudents(st1, ARRAY_SIZE(st1), sa1, + ARRAY_SIZE(sa1))); // expect: 0 + printf("%d\n", countStudents(st2, ARRAY_SIZE(st2), sa2, + ARRAY_SIZE(sa2))); // expect: 3 +} |