# Code language: Python classSolution: defcountStudents(self, students: List[int], sandwiches: List[int]) -> int: n = len(students) st = deque(range(n)) for i, s inenumerate(sandwiches): for j inrange(i, n): if students[st[0]] == s: break st.rotate(-1) if students[st[0]] != s: return n - i st.popleft() return0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Code language: Java classSolution { publicintcountStudents(int[] students, int[] sandwiches) { intn= students.length; Deque<Integer> st = newArrayDeque<>(); for (inti=0; i < n; ++i) st.addLast(i); for (inti=0; i < n; ++i) { for (intj= i; j < n && students[st.peekFirst()] != sandwiches[i]; ++j) st.addLast(st.pollFirst()); if (students[st.peekFirst()] != sandwiches[i]) { return n - i; } st.pollFirst(); } return0; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Code language: C++ classSolution { public: intcountStudents(vector<int>& students, vector<int>& sandwiches){ int n = students.size(); deque<int> st; for (int i = 0; i < n; ++i) st.emplace_back(i); for (int i = 0; i < n; ++i) { for (int j = i; j < n && students[st.front()] != sandwiches[i]; ++j) { st.emplace_back(st.front()); st.pop_front(); } if (students[st.front()] != sandwiches[i]) { return n - i; } st.pop_front(); } return0; } };