{"id":7840,"date":"2020-12-26T16:28:07","date_gmt":"2020-12-27T00:28:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7840"},"modified":"2020-12-26T16:50:34","modified_gmt":"2020-12-27T00:50:34","slug":"leetcode-1700-number-of-students-unable-to-eat-lunch","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1700-number-of-students-unable-to-eat-lunch\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1700. Number of Students Unable to Eat Lunch"},"content":{"rendered":"\n<p>The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers&nbsp;<code>0<\/code>&nbsp;and&nbsp;<code>1<\/code>&nbsp;respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.<\/p>\n\n\n\n<p>The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a&nbsp;<strong>stack<\/strong>. At each step:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the student at the front of the queue&nbsp;<strong>prefers<\/strong>&nbsp;the sandwich on the top of the stack, they will&nbsp;<strong>take it<\/strong>&nbsp;and leave the queue.<\/li><li>Otherwise, they will&nbsp;<strong>leave it<\/strong>&nbsp;and go to the queue&#8217;s end.<\/li><\/ul>\n\n\n\n<p>This continues until none of the queue students want to take the top sandwich and are thus unable to eat.<\/p>\n\n\n\n<p>You are given two integer arrays&nbsp;<code>students<\/code>&nbsp;and&nbsp;<code>sandwiches<\/code>&nbsp;where&nbsp;<code>sandwiches[i]<\/code>&nbsp;is the type of the&nbsp;<code>i<sup>\u200b\u200b\u200b\u200b\u200b\u200bth<\/sup><\/code>&nbsp;sandwich in the stack (<code>i = 0<\/code>&nbsp;is the top of the stack) and&nbsp;<code>students[j]<\/code>&nbsp;is the preference of the&nbsp;<code>j<sup>\u200b\u200b\u200b\u200b\u200b\u200bth<\/sup><\/code>&nbsp;student in the initial queue (<code>j = 0<\/code>&nbsp;is the front of the queue). Return&nbsp;<em>the number of students that are unable to eat.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> students = [1,1,0,0], sandwiches = [0,1,0,1]\n<strong>Output:<\/strong> 0<strong> \nExplanation:<\/strong>\n- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].\n- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].\n- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].\n- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].\n- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].\n- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].\n- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].\n- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].\nHence all students are able to eat.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= students.length, sandwiches.length &lt;= 100<\/code><\/li><li><code>students.length == sandwiches.length<\/code><\/li><li><code>sandwiches[i]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><li><code>students[i]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countStudents(vector<int>& students, vector<int>& sandwiches) {\n    queue<int> q;\n    for (int s : students) q.push(s);\n    int c = 0;\n    int i = 0;\n    while (!q.empty()) {\n      int s = q.front(); q.pop();      \n      if (s == sandwiches[i]) {\n        ++i;\n        c = 0;\n      } else {\n        q.push(s);\n      }\n      if (++c > q.size()) break;\n    }\n    return q.size();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Counting<\/strong><\/h2>\n\n\n\n<p>Count student&#8217;s preferences. Then process students from 1 to n, if there is no sandwich for current student then we can stop, since he\/she will block all the students behind him\/her.  <\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countStudents(vector<int>& students, vector<int>& sandwiches) {\n    const int n = students.size();\n    vector<int> c(2);\n    for (int p : students) ++c[p];\n    for (int i = 0; i < n; ++i)\n      if (--c[sandwiches[i]] < 0) return n - i;\n    return 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers&nbsp;0&nbsp;and&nbsp;1&nbsp;respectively. All students stand in a queue. Each student either prefers&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,213,179],"class_list":["post-7840","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-queue","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7840"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7840\/revisions"}],"predecessor-version":[{"id":7842,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7840\/revisions\/7842"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}