| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.NumberOfStudentsUnableToEatLunch; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class NumberOfStudentsUnableToEatLunchQueue : INumberOfStudentsUnableToEatLunch |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="students"></param> |
| | 22 | | /// <param name="sandwiches"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int CountStudents(int[] students, int[] sandwiches) |
| 2 | 25 | | { |
| 2 | 26 | | var studentsQueue = new Queue<int>(students); |
| | 27 | |
|
| 2 | 28 | | var sandwichesIndex = 0; |
| | 29 | |
|
| 2 | 30 | | var count = 0; |
| | 31 | |
|
| 18 | 32 | | while (studentsQueue.Count > 0 && sandwichesIndex < sandwiches.Length && |
| 18 | 33 | | count < sandwiches.Length - sandwichesIndex) |
| 16 | 34 | | { |
| 16 | 35 | | var student = studentsQueue.Dequeue(); |
| 16 | 36 | | var sandwich = sandwiches[sandwichesIndex]; |
| | 37 | |
|
| 16 | 38 | | if (student == sandwich) |
| 7 | 39 | | { |
| 7 | 40 | | count = 0; |
| 7 | 41 | | sandwichesIndex++; |
| 7 | 42 | | } |
| | 43 | | else |
| 9 | 44 | | { |
| 9 | 45 | | count++; |
| 9 | 46 | | studentsQueue.Enqueue(student); |
| 9 | 47 | | } |
| 16 | 48 | | } |
| | 49 | |
|
| 2 | 50 | | return studentsQueue.Count; |
| 2 | 51 | | } |
| | 52 | | } |