Pass the Pillow

To solve this problem, we can use a simulation approach where we keep track of the current position and direction of the pillow. We can then update the position of the pillow every second according to the direction until the specified time is reached.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
    def passThePillow(self, n: int, time: int) -> int:
        # Initialize the position and direction of the pillow
        position = 1
        direction = 1

        # Pass the pillow for the given amount of time
        for _ in range(time):
            position += direction

            # If the pillow reaches the end of the line, change the direction
            if position == n:
                direction = -1
            elif position == 1:
                direction = 1

        # Return the position of the person holding the pillow
        return position

In this function, position represents the current position of the pillow, and direction represents the direction in which the pillow is passed (1 for forward and -1 for backward). The function then updates the position of the pillow every second according to the direction. If the pillow reaches the end of the line (either the first person or the nth person), the direction is changed. Finally, the function returns the position of the person holding the pillow after the specified time.