Queue : April POTD

Queue : April POTD

Day 12/75:

Question: Number of Students Unable to Eat Lunch -(Easy)

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 ith sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the jth student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

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.        

Approach:

  1. Create a queue (studentQueue) to represent the line of students.
  2. Iterate through the students array and add each student's preference to the queue.
  3. Initialize a variable rotation to keep track of how many times we rotate the queue without any student taking a sandwich.
  4. Iterate through the queue until it's empty or until we've rotated it without any student taking a sandwich.Check if the top student in the queue prefers the current sandwich.If they do, remove them from the queue and increment the index of the current sandwich (i).Reset the rotation counter.If not, remove the top student from the queue and add them to the end of the queue.Increment the rotation counter.
  5. Return the size of the queue, indicating the number of students unable to eat.

CODE:

Time Complexity: O(n^2)

  • In the worst case, we iterate through the queue n times, and each time, we may rotate the queue up to n times.
  • Therefore, the time complexity is O(n^2).

Space Complexity: O(n)

  • We use a queue to store the students' preferences, requiring O(n) space.

?? Kudos, Suyash Awathe for cracking the code to success once again! Your recent achievement is a testament to your sharp intellect and problem-solving prowess. With each challenge conquered, you pave the way for new possibilities and inspire others to follow in your footsteps. Keep decoding greatness! ????

回复

要查看或添加评论,请登录

Suyash Awathe的更多文章

  • Tree: Add One Row to Tree

    Tree: Add One Row to Tree

    Day 17: Problem Statement: Given the root of a binary tree and two integers val and depth, add a row of nodes with…

  • Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Day 16/75 : Problem : Trapping Rain Water -(Hard) Given n non-negative integers representing an elevation map where the…

  • Stack: April POTD

    Stack: April POTD

    Question: Remove K Digits -(Medium) Given string num representing a non-negative integer num, and an integer k, return…

  • Queue-Array: April POTD

    Queue-Array: April POTD

    Day 14/75: Question: Reveal Cards In Increasing Order -(Medium) You are given an integer array . There is a deck of…

    1 条评论
  • Array: 9 April POTD

    Array: 9 April POTD

    Day 13/75: Question: Time Needed to Buy Tickets -(Easy) There are people in a line queuing to buy tickets, where the…

  • String : Parentheses : April-POTD

    String : Parentheses : April-POTD

    Day 11/75: Question: Minimum Remove to Make Valid Parentheses -(Medium) Given a string s of , and lowercase English…

    3 条评论
  • String : 5April POTD

    String : 5April POTD

    Day 10/75: Question: Make The String Great -(Easy) Given a string of lower and upper case English letters. A good…

  • String Parentheses : 4April POTD

    String Parentheses : 4April POTD

    Day 9/75: Question: Maximum Nesting Depth of the Parentheses -(Easy) A string is a valid parentheses string (denoted…

  • BackTracking : WordSearch :4April POTD

    BackTracking : WordSearch :4April POTD

    Day 9/75: Question: Word Search -(Medium) Given an m x n grid of characters board and a string word, return true if…

    1 条评论
  • Isomorphic String : April POTD

    Isomorphic String : April POTD

    Day 8/75: Question: Given two strings s and t, determine if they are isomorphic. Two strings and are isomorphic if the…

    2 条评论

社区洞察

其他会员也浏览了