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.
|
|
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.