LeetCode 1700: Number of Students Unable to Eat Lunch Solution

Master LeetCode problem 1700 (Number of Students Unable to Eat Lunch), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1700. Number of Students Unable to Eat Lunch

Problem Explanation

Explanation:

  • We can simulate the process of students taking sandwiches by iterating over the students and sandwiches arrays.
  • We will keep track of the index of the next sandwich to be taken.
  • If a student prefers the current sandwich, they will take it and move to the next sandwich.
  • If a student does not prefer the current sandwich, they will be moved to the end of the queue.
  • We repeat this process until all students have either taken a sandwich or are unable to eat.
  • The number of students unable to eat is the remaining number of students in the queue.

Solution Code

class Solution {
    public int countStudents(int[] students, int[] sandwiches) {
        int[] count = new int[2];
        for (int student : students) {
            count[student]++;
        }
        
        for (int sandwich : sandwiches) {
            if (count[sandwich] == 0) {
                break;
            }
            count[sandwich]--;
        }
        
        return count[0] + count[1];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1700 (Number of Students Unable to Eat Lunch)?

This page provides optimized solutions for LeetCode problem 1700 (Number of Students Unable to Eat Lunch) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1700 (Number of Students Unable to Eat Lunch)?

The time complexity for LeetCode 1700 (Number of Students Unable to Eat Lunch) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1700 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1700 in Java, C++, or Python.

Back to LeetCode Solutions