Pass the Pillow (LeetCode 2582): Solution and Explanation
A pillow bounces back and forth along a line of n people, one pass per second. Find who holds it after time seconds. Split the time into full end-to-end trips and a remainder, and the parity of the trip count tells you which end to count from.
Intuition
Simulating works because time is at most 1000, but the motion has an obvious period. Walking from person 1 to person n takes n - 1 passes, and so does walking back.
After any whole number of trips the pillow sits at an end. An even count of trips brings it back to person 1, heading right. An odd count leaves it at person n, heading left.
The leftover seconds, time % (n - 1), are fewer than one full trip, so they never cause another bounce. Count them forward from 1 or backward from n.
Approach: Trips and remainder
- Compute trips = time // (n - 1) and rem = time % (n - 1).
- If trips is even, the pillow is at person 1 moving right: return 1 + rem.
- Otherwise it is at person n moving left: return n - rem.
Step-by-step example
n = 4, time = 5
- One trip takes n - 1 = 3 seconds.
- 5 = 1 * 3 + 2, so there is 1 full trip and 2 leftover seconds.
- 1 trip is odd: after second 3 the pillow is at person 4, moving left.
- Two more passes: 4 -> 3 -> 2. Return 4 - 2 = 2.
Code: Python, Java, C++
1class Solution:
2 def passThePillow(self, n: int, time: int) -> int:
3 trips, rem = divmod(time, n - 1)
4 if trips % 2 == 0:
5 return 1 + rem
6 return n - rem1class Solution {
2 public int passThePillow(int n, int time) {
3 int trips = time / (n - 1), rem = time % (n - 1);
4 if (trips % 2 == 0) return 1 + rem;
5 return n - rem;
6 }
7}1class Solution {
2public:
3 int passThePillow(int n, int time) {
4 int trips = time / (n - 1), rem = time % (n - 1);
5 if (trips % 2 == 0) return 1 + rem;
6 return n - rem;
7 }
8};Time and space complexity
O(1)
One division, one modulo and a comparison.
O(1)
Two integers.
Other approaches
Simulate each second (time O(time), space O(1))
Keep a position and a direction, step once per second, and flip the direction at either end. It is O(time) and perfectly fine for time up to 1000, and a good way to check the formula.
Reduce modulo the full cycle (time O(1), space O(1))
A round trip takes 2(n - 1) seconds. Let r = time % (2(n - 1)); the answer is 1 + r if r < n, else 2n - 1 - r. It is the same idea with one modulo instead of a parity check.
Common mistakes
- Using n instead of n - 1 as the trip length. There are n people but only n - 1 passes between the ends.
- Flipping direction one second late in a simulation, so the pillow steps past the end of the line.
- Forgetting that a remainder of 0 means the pillow is exactly at an end, which the formula already handles (1 + 0 or n - 0).
Frequently asked questions
Why does an even number of trips put the pillow at person 1?
Each trip goes from one end to the other. Two trips make a round trip back to the start, so any even count ends at person 1 and any odd count at person n.
Can the remainder cause another bounce?
No. The remainder is less than n - 1, the length of one trip, so the pillow cannot reach the far end during it.
What happens when n = 2?
Each trip is a single pass, so the pillow alternates between the two people: person 2 after odd seconds and person 1 after even seconds.