Number of Students Unable to Eat Lunch

🏠 ⬅️ ➑️

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. The sandwiches are placed in a stack. At each step:

  • If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
  • Otherwise, they will leave it and go to the queue's end.

This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

Example 1:

Input: students = [1,1,0,0], sandwiches = [0,1,0,1] Output: 0 Explanation: - Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1]. - Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1]. - Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1]. - Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0]. - Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1]. - Front student leaves the top sandwich and returns to the end of the line making students = [0,1]. - Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1]. - Front student takes the top sandwich and leaves the line making students = [] and sandwiches = []. Hence all students are able to eat.

Example 2:

Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1] Output: 3

Constraints:

  • 1 <= students.length, sandwiches.length <= 100
  • students.length == sandwiches.length
  • sandwiches[i] is 0 or 1.
  • students[i] is 0 or 1.

Note: This problem is from LeetCode.
βœ… Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer :: n_students, n_sandwiches
    integer, dimension(:), allocatable :: students, sandwiches
    integer :: i, j, k, l

    ! read input
    read(*,*) n_students, n_sandwiches
    allocate(students(n_students))
    allocate(sandwiches(n_sandwiches))
    do i = 1, n_students
        read(*,*) students(i)
    end do
    do i = 1, n_sandwiches
        read(*,*) sandwiches(i)
    end do

    ! solve problem
    do while (any(students /= 0))
        if (students(1) == sandwiches(1)) then
            ! student at front of queue prefers top sandwich
            students(1) = 0
            sandwiches(1) = 0
        else
            ! student at front of queue does not prefer top sandwich
            ! move them to the back of the queue
            do i = 1, n_students - 1
                students(i) = students(i + 1)
            end do
            students(n_students) = 0
        end if
    end do

    ! print output
    write(*,*) "Number of students that are unable to eat: ", count(students /= 0)

end program main
βœ… Compiled
❌ Executed
❌ Correct
program main
      implicit none

      integer, parameter :: MAX_STUDENTS = 100
      integer, parameter :: MAX_SANDWICHES = 100

      integer :: students(MAX_STUDENTS)
      integer :: sandwiches(MAX_SANDWICHES)
      integer :: num_students, num_sandwiches
      integer :: i, j

      read (*,*) num_students, num_sandwiches

      do i = 1, num_students
         read (*,*) students(i)
      end do

      do i = 1, num_sandwiches
         read (*,*) sandwiches(i)
      end do

      do i = 1, num_students
         if (students(i) == sandwiches(1)) then
            write (*,*) "Student ", i, " takes the top sandwich"
            students(i) = 0
            sandwiches(1) = 0
         else
            write (*,*) "Student ", i, " leaves the top sandwich"
            students(i) = 0
         end if
      end do

      do i = 1, num_sandwiches
         if (sandwiches(i) == 1) then
            write (*,*) "Sandwich ", i, " is taken"
         else
            write (*,*) "Sandwich ", i, " is left"
         end if
      end do

      do i = 1, num_students
         if (students(i) == 1) then
            write (*,*) "Student ", i, " is unable to eat"
         end if
      end do

      end program main
🌐 Data from online sources
def min_time_to_remove_balloons(colors, neededTime):
    n = len(colors)
    INF = 10**9
    dp = [[INF] * 26 for _ in range(n)]

    for color in range(26):
        if colors[0] != chr(ord('A') + color):
            dp[0][color] = neededTime[0]

    for i in range(1, n):
        for color1 in range(26):
            for color2 in range(26):
                if color1 != color2 and colors[i] != chr(ord('A') + color1):
                    dp[i][color1] = min(dp[i][color1], dp[i-1][color2] + neededTime[i])

    ans = INF
    for color in range(26):
        ans = min(ans, dp[n-1][color])

    return ans
The algorithm uses dynamic programming. We maintain a 2D table dp[i][j] that represents the minimum time Bob needs to make the rope colorful up to (and including) the i-th balloon, with the assumption that the i-th balloon's color is changed to color j. Initially, we fill the table with a large value (here, 1e9).

We then loop through each balloon, and for each balloon, we loop through all pairs of colors (26^2 possibilities). For each pair of colors, if the colors are different and the i-th balloon is not of color1, we update dp[i][color1] by taking the minimum between its current value and dp[i - 1][color2] + the time needed to remove the i-th balloon.

After iterating over all balloons and color pairs, we search for the minimum value in the last row of the dp table, as this represents the minimum time needed to make the whole rope colorful.

The time complexity of this algorithm is O(n * 26^2), where n is the length of the string colors, since the algorithm needs to loop through each balloon and all possible color pairs. This complexity is acceptable for the problem's constraints.

🌐 Data from online sources
#include <vector>
#include <string>

int min_time_to_remove_balloons(const std::string& colors, const std::vector<int>& neededTime) {
    int n = colors.size();
    int dp[n][26];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 26; j++) {
            dp[i][j] = 1e9; 
        }
    }

    for (int color = 0; color < 26; color++) {
        if (colors[0] != 'A' + color) dp[0][color] = neededTime[0];
    }

    for (int i = 1; i < n; i++) {
        for (int color1 = 0; color1 < 26; color1++) {
            for (int color2 = 0; color2 < 26; color2++) {
                if (color1 != color2 && colors[i] != 'A' + color1) {
                    dp[i][color1] = std::min(dp[i][color1], dp[i-1][color2] + neededTime[i]);
                }
            }
        }
    }

    int ans = 1e9;
    for (int color = 0; color < 26; color++) {
        ans = std::min(ans, dp[n-1][color]);
    }
    return ans;
}
The algorithm uses dynamic programming. We maintain a 2D table dp[i][j] that represents the minimum time Bob needs to make the rope colorful up to (and including) the i-th balloon, with the assumption that the i-th balloon's color is changed to color j. Initially, we fill the table with a large value (here, 1e9).

We then loop through each balloon, and for each balloon, we loop through all pairs of colors (26^2 possibilities). For each pair of colors, if the colors are different and the i-th balloon is not of color1, we update dp[i][color1] by taking the minimum between its current value and dp[i - 1][color2] + the time needed to remove the i-th balloon.

After iterating over all balloons and color pairs, we search for the minimum value in the last row of the dp table, as this represents the minimum time needed to make the whole rope colorful.

The time complexity of this algorithm is O(n * 26^2), where n is the length of the string colors, since the algorithm needs to loop through each balloon and all possible color pairs. This complexity is acceptable for the problem's constraints.