Similar Problems

Similar Problems not available

Pass The Pillow - Leetcode Solution

Companies:

LeetCode:  Pass The Pillow Leetcode Solution

Difficulty: Easy

Topics: math simulation  

Problem Statement:

You are given a list of n people, numbered from 0 to n-1, and a list of k integer pairs representing pieces of information being passed between them. Each pair (a,b) represents that person a has some information to pass to person b.

The initial person to hold a pillow is person 0, and each time someone passes a piece of information, they pass the pillow along with it to the recipient. If someone wants to pass the pillow without passing any information, they can do so.

Return the number of times the pillow is passed in a circle until it gets back to person 0, starting from person 0 and assuming the pillow is initially held by person 0.

Example:

Input: n = 4, knowledge = [[0,1],[1,2],[2,3],[3,0]] Output: 4 Explanation:

  • Person 0 starts with the pillow.
  • Person 0 gives the pillow and piece of information to person 1.
  • Person 1 gives the pillow and piece of information to person 2.
  • Person 2 gives the pillow and piece of information to person 3.
  • Person 3 gives the pillow and piece of information to person 0. Hence, the output is 4.

Solution:

To solve the given problem, we can make use of a greedy approach. We can start with person 0 holding the pillow and passing it along with the information to the next person. We can maintain a count to keep track of the number of times the pillow is passed in a circle until it gets back to person 0. We can repeat the process until the pillow is back with person 0.

We can make use of a dictionary to store the information passed between the people. The keys of the dictionary would be the people, and the values would be a set containing information passed to that person. We can make use of a queue to keep track of the people to whom the pillow has been passed along with the information.

Below is the step by step approach to solve the given problem:

  1. Initialize a dictionary, info_dict, to store the information passed between the people. The keys would be the people, and the values would be a set containing information passed to that person.

  2. Initialize a queue, q, with person 0.

  3. Initialize a count, pass_count, to keep track of the number of times the pillow is passed in a circle until it gets back to person 0.

  4. While the queue is not empty, do the following: a) Pop the first person, curr_person, from the queue. b) For each pair (curr_person, next_person) in the knowledge list: i) If the next_person has not received any information so far, add the next_person to the queue and increment the pass_count. ii) If the next_person has already received information from curr_person, do nothing. iii) If the next_person has not received information from curr_person, add the information to the info_dict and add the next_person to the queue and increment the pass_count. c) After processing all the pairs in the knowledge list, check if the pillow has been passed back to person 0. If yes, return the pass_count.

  5. If the pillow has not been passed back to person 0, return -1 as the answer.

Time Complexity:

The time complexity of the above solution is O(n * k), where n is the number of people and k is the number of information exchange pairs.

Space Complexity:

The space complexity of the above solution is also O(n * k), as we are using a dictionary to store the information passed between the people.

Pass The Pillow Solution Code

1