🔥🔥🔥 Explained Easy Queue and Stack Solution 🔥🔥🔥
Intuition\nTo approach this problem, it\'s crucial to understand how a queue (FIFO - First In First Out) and a stack (LIFO - Last In First Out) work. The queue 
# Intuition\nTo approach this problem, it\'s crucial to understand how a queue (FIFO - First In First Out) and a stack (LIFO - Last In First Out) work. The queue represents the line of students, while the stack represents the pile of sandwiches. The student at the front of the queue will always try to pick the sandwich at the top of the stack. If the sandwich matches the student\'s preference, they take it and leave the queue. If not, the student moves to the back of the queue, and the next student in line gets a chance to pick a sandwich.\n\n# Approach\n**1. Initialization:** Convert the **students** array into a queue and the **sandwiches** array into a stack. This is done to simulate the real-world scenario of a line of students and a stack of sandwiches.\n\n**2. Processing the Queue and Stack:**\n    - We continuously iterate over the queue of students and the stack of sandwiches.\n    - During each iteration, we check if the preference of the student at the front of the queue matches the type of the sandwich on top of the stack.\n    - If it matches, the student takes the sandwich (dequeues and the sandwich is popped from the stack), and we reset the counter because a sandwich has been successfully taken.\n    - If it doesn\'t match, the student moves to the end of the queue (dequeues and then enqueues the same student), and we increment a counter. This counter is crucial as it helps us identify if we have gone through the entire queue without any student taking a sandwich.\n\n**3. Termination Condition:**\n    - The loop terminates when the counter becomes equal to the size of the food stack. This means that we\'ve cycled through all students without any of them taking the top sandwich, indicating that the remaining students cannot pick any more sandwiches.\n\n**4. Returning the Result:**\n    - After the loop terminates, the counter will hold the number of students who couldn\'t get their preferred sandwich, which is the answer we return.\n\n# Complexity\n- Time complexity:\n    **1. Initializing Queues and Stacks:** The loop to fill **studentsQueue** and **foodStack** runs for **n** iterations, where **n** is the number of students/sandwiches. This part is **O(n)**.\n\n    **2. Main While Loop:** The **while** loop can run a maximum of **n** iterations for each sandwich (in the worst-case scenario). In the worst case, each student will be dequeued and enqueued once per sandwich, leading to **n^2** operations.\n\n    Therefore, the time complexity is **O(n^2)**.\n\n- Space complexity:\n    **1. Queue and Stack Storage:** Both the queue and stack store n elements each, contributing **O(n)** space complexity.\n\n# Code\n```javascript []\n/**\n * @param {number[]} students\n * @param {number[]} sandwiches\n * @return {number}\n */\n class QueueNode {\n    constructor(data) {\n        this.data = data;\n        this.next = null;\n    }\n}\n\nclass Queues {\n    constructor() {\n        this.head = null;\n        this.tail = null;\n        this.size = 0;\n    }\n\n    enqueue(data) {\n        let newNode = new QueueNode(data);\n\n        if (this.tail === null) {\n            this.head = this.tail = newNode;\n        } else {\n            this.tail.next = newNode;\n            this.tail = newNode;\n        }\n        \n        this.size++;\n    }\n\n    dequeue() {\n        if (this.isEmpty()) {\n            return null;\n        }\n\n        let removedNode = this.head;\n        this.head = this.head.next;\n\n        if (this.head === null) {\n            this.tail = null;\n        }\n\n        this.size--;\n        return removedNode.data;\n    }\n\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    peek() {\n        if (this.isEmpty()) {\n            return null;\n        }\n        return this.head.data;\n    }\n\n    getSize() {\n        return this.size;\n    }\n}\nvar countStudents = function(students, sandwiches) {\n    const len = students.length\n    const studentsQueue = new Queues()\n    const foodStack = new Array()\n\n    for (let i = 0 ; i < len ; i++){\n        studentsQueue.enqueue(students[i])\n        foodStack.push(sandwiches[len - 1 - i])\n    }\n\n    let counter = 0\n    while(counter<foodStack.length){\n        if(studentsQueue.peek() == foodStack[foodStack.length - 1]){\n            studentsQueue.dequeue()\n            foodStack.pop()\n            counter = 0\n        }else{\n            const deqVal = studentsQueue.dequeue()\n            studentsQueue.enqueue(deqVal)\n            counter++\n        }\n    }\n    return counter\n};\n```\n```python3 []\nclass QueueNode:\n    def __init__(self, data):\n        self.data = data\n        self.next = None\n\nclass Queues:\n    def __init__(self):\n        self.head = None\n        self.tail = None\n        self.size = 0\n\n    def enqueue(self, data):\n        new_node = QueueNode(data)\n\n        if self.tail is None:\n            self.head = self.tail = new_node\n        else:\n            self.tail.next = new_node\n            self.tail = new_node\n\n        self.size += 1\n\n    def dequeue(self):\n        if self.is_empty():\n            return None\n\n        removed_node = self.head\n        self.head = self.head.next\n\n        if self.head is None:\n            self.tail = None\n\n        self.size -= 1\n        return removed_node.data\n\n    def is_empty(self):\n        return self.size == 0\n\n    def peek(self):\n        if self.is_empty():\n            return None\n        return self.head.data\n\n    def get_size(self):\n        return self.size\n\nclass Solution:\n    def countStudents(self, students, sandwiches):\n        len_students = len(students)\n        students_queue = Queues()\n        food_stack = []\n\n        for i in range(len_students):\n            students_queue.enqueue(students[i])\n            food_stack.append(sandwiches[len_students - 1 - i])\n\n        counter = 0\n        while counter < len(food_stack):\n            if students_queue.peek() == food_stack[-1]:\n                students_queue.dequeue()\n                food_stack.pop()\n                counter = 0\n            else:\n                deq_val = students_queue.dequeue()\n                students_queue.enqueue(deq_val)\n                counter += 1\n\n        return counter\n\n```\n```java []\nimport java.util.LinkedList;\nimport java.util.Queue;\nimport java.util.Stack;\n\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int lenStudents = students.length;\n        Queue<Integer> studentsQueue = new LinkedList<>();\n        Stack<Integer> foodStack = new Stack<>();\n\n        for (int i = 0; i < lenStudents; i++) {\n            studentsQueue.offer(students[i]);\n            foodStack.push(sandwiches[lenStudents - 1 - i]);\n        }\n\n        int counter = 0;\n        while (counter < foodStack.size()) {\n            if (studentsQueue.peek().equals(foodStack.peek())) {\n                studentsQueue.poll();\n                foodStack.pop();\n                counter = 0;\n            } else {\n                int deqVal = studentsQueue.poll();\n                studentsQueue.offer(deqVal);\n                counter++;\n            }\n        }\n\n        return counter;\n    }\n}\n\nclass MyStack {\n    private Queue<Integer> mainQueue;\n    private Queue<Integer> tempQueue;\n\n    public MyStack() {\n        mainQueue = new LinkedList<>();\n        tempQueue = new LinkedList<>();\n    }\n\n    public void push(int x) {\n        tempQueue.offer(x);\n        while (!mainQueue.isEmpty()) {\n            tempQueue.offer(mainQueue.poll());\n        }\n        Queue<Integer> temp = mainQueue;\n        mainQueue = tempQueue;\n        tempQueue = temp;\n    }\n\n    public int pop() {\n        if (mainQueue.isEmpty()) {\n            return -1; // Return a default value for an empty stack\n        }\n        return mainQueue.poll();\n    }\n\n    public int top() {\n        if (mainQueue.isEmpty()) {\n            return -1; // Return a default value for an empty stack\n        }\n        return mainQueue.peek();\n    }\n\n    public boolean empty() {\n        return mainQueue.isEmpty();\n    }\n}\n```\n```C++ []\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nclass QueueNode {\npublic:\n    int data;\n    QueueNode* next;\n\n    QueueNode(int val) {\n        data = val;\n        next = nullptr;\n    }\n};\n\nclass Queues {\npublic:\n    QueueNode* head;\n    QueueNode* tail;\n    int size;\n\n    Queues() {\n        head = nullptr;\n        tail = nullptr;\n        size = 0;\n    }\n\n    void enqueue(int data) {\n        QueueNode* newNode = new QueueNode(data);\n\n        if (tail == nullptr) {\n            head = tail = newNode;\n        } else {\n            tail->next = newNode;\n            tail = newNode;\n        }\n\n        size++;\n    }\n\n    int dequeue() {\n        if (is_empty()) {\n            return -1; // Return a default value for an empty queue\n        }\n\n        QueueNode* removedNode = head;\n        head = head->next;\n\n        if (head == nullptr) {\n            tail = nullptr;\n        }\n\n        size--;\n        int data = removedNode->data;\n        delete removedNode;\n        return data;\n    }\n\n    bool is_empty() {\n        return size == 0;\n    }\n\n    int peek() {\n        if (is_empty()) {\n            return -1; // Return a default value for an empty queue\n        }\n        return head->data;\n    }\n\n    int get_size() {\n        return size;\n    }\n};\n\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int len_students = students.size();\n        Queues students_queue;\n        vector<int> food_stack;\n\n        for (int i = 0; i < len_students; i++) {\n            students_queue.enqueue(students[i]);\n            food_stack.push_back(sandwiches[len_students - 1 - i]);\n        }\n\n        int counter = 0;\n        while (counter < food_stack.size()) {\n            if (students_queue.peek() == food_stack.back()) {\n                students_queue.dequeue();\n                food_stack.pop_back();\n                counter = 0;\n            } else {\n                int deq_val = students_queue.dequeue();\n                students_queue.enqueue(deq_val);\n                counter++;\n            }\n        }\n\n        return counter;\n    }\n};\n\nclass MyStack {\npublic:\n    Queues mainQueue;\n    Queues tempQueue;\n\n    MyStack() {}\n\n    void push(int x) {\n        tempQueue.enqueue(x);\n        while (!mainQueue.is_empty()) {\n            tempQueue.enqueue(mainQueue.dequeue());\n        }\n        swap(mainQueue, tempQueue);\n    }\n\n    int pop() {\n        if (mainQueue.is_empty()) {\n            return -1; // Return a default value for an empty stack\n        }\n        return mainQueue.dequeue();\n    }\n\n    int top() {\n        if (mainQueue.is_empty()) {\n            return -1; // Return a default value for an empty stack\n        }\n        return mainQueue.peek();\n    }\n\n    bool empty() {\n        return mainQueue.is_empty();\n    }\n};\n```
['Stack', 'Queue', 'C++', 'Java', 'Python3', 'JavaScript']