Skip to content

Commit 7e90b21

Browse files
authored
Merge pull request #2635 from somilyadav7/2582
Added Solution to Leetcode 2582
2 parents 5ed1988 + 02a4884 commit 7e90b21

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
id: pass-the-pillow
3+
title: Pass the Pillow
4+
sidebar_label: 2582. Pass the Pillow
5+
tags:
6+
- Math
7+
- Simulation
8+
description: "Solution to Leetcode 2582. Pass the Pillow "
9+
---
10+
11+
## Problem Description
12+
13+
There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.
14+
15+
For example, once the pillow reaches the nth person they pass it to the $(n-1)$ th person, then to the $(n-2)$ th person and so on.
16+
Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.
17+
18+
19+
### Examples
20+
21+
**Example 1:**
22+
23+
```
24+
Input: n = 4, time = 5
25+
Output: 2
26+
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
27+
After five seconds, the 2nd person is holding the pillow.
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Input: n = 3, time = 2
34+
Output: 3
35+
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
36+
After two seconds, the 3rd person is holding the pillow.
37+
38+
```
39+
40+
41+
42+
### Constraints
43+
- `2 <= n <= 1000`
44+
- `1 <= time <= 1000`
45+
46+
### Approach
47+
1. To understand how the pillow moves among the people in line, let understand the pattern of its movement. The pillow completes a full round when it travels from the first person to the last or vice versa. Each complete round takes $(n-1)$ seconds, where n is the total number of people.
48+
49+
To determine how many complete rounds the pillow makes within a given time time, we divide time by $(n-1)$. This gives us fullRounds, representing the number of times the pillow moves from one end of the line to the other. The remainder of this division, extraTime = time % (n - 1), indicates the extra time left after completing these full rounds.
50+
51+
Now, let's consider the direction of the pillow's movement:
52+
53+
- If fullRounds is even, the pillow moves forward along the line.
54+
- If fullRounds is odd, the pillow moves backward. This directional change occurs after each complete round.
55+
- In the case of forward movement (fullRounds is even), the person holding the pillow after the extra time will be positioned at extraTime + 1 (since we start counting positions from one). Conversely, during backward movement (fullRounds is odd), the person holding the pillow will be at position n - extraTime
56+
57+
### Complexity
58+
59+
Time complexity: $O(1)$.Regardless of the size of the input, we always perform a fixed number of operations thus the time complexity is $O(1)$.
60+
61+
Space complexity: $O(1)$.Regardless of the size of the input, we only use a fixed number of auxiliary variables, thus the space complexity is $O(1)$.
62+
63+
64+
### Solution
65+
66+
#### Code in Different Languages
67+
68+
#### C++
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
int passThePillow(int n, int time) {
74+
// Calculate the number of complete rounds of pillow passing
75+
int fullRounds = time / (n - 1);
76+
77+
// Calculate the remaining time after complete rounds
78+
int extraTime = time % (n - 1);
79+
80+
// Determine the position of the person holding the pillow
81+
// If fullRounds is even, the pillow is moving forward.
82+
// If fullRounds is odd, the pillow is moving backward.
83+
if (fullRounds % 2 == 0) {
84+
return extraTime + 1; // Position when moving forward
85+
} else {
86+
return n - extraTime; // Position when moving backward
87+
}
88+
}
89+
};
90+
```
91+
92+
#### JAVA
93+
94+
```java
95+
class Solution {
96+
97+
public int passThePillow(int n, int time) {
98+
// Calculate the number of complete rounds of pillow passing
99+
int fullRounds = time / (n - 1);
100+
101+
// Calculate the remaining time after complete rounds
102+
int extraTime = time % (n - 1);
103+
104+
// Determine the position of the person holding the pillow
105+
// If fullRounds is even, the pillow is moving forward.
106+
// If fullRounds is odd, the pillow is moving backward.
107+
if (fullRounds % 2 == 0) {
108+
return extraTime + 1; // Position when moving forward
109+
} else {
110+
return n - extraTime; // Position when moving backward
111+
}
112+
}
113+
}
114+
```
115+
116+
#### PYTHON
117+
118+
```python
119+
class Solution:
120+
def passThePillow(self, n, time):
121+
# Calculate the number of complete rounds of pillow passing
122+
full_rounds = time // (n - 1)
123+
124+
# Calculate the remaining time after complete rounds
125+
extra_time = time % (n - 1)
126+
127+
# Determine the position of the person holding the pillow
128+
# If full_rounds is even, the pillow is moving forward.
129+
# If full_rounds is odd, the pillow is moving backward.
130+
if full_rounds % 2 == 0:
131+
return extra_time + 1 # Position when moving forward
132+
else:
133+
return n - extra_time # Position when moving backward
134+
135+
```
136+
137+
138+
139+
### Complexity Analysis
140+
141+
- Time Complexity: $O(1)$
142+
143+
- Space Complexity: $O(1)$
144+
145+
### References
146+
147+
- **LeetCode Problem**: Pass the Pillow

0 commit comments

Comments
 (0)