|
1 | 1 | # [Problem 2582: Pass the Pillow](https://leetcode.com/problems/pass-the-pillow)
|
2 | 2 |
|
3 | 3 | ## Initial thoughts (stream-of-consciousness)
|
| 4 | + - I think this is pretty straightforward |
| 5 | + - For a list of length $n$, it takes $n - 1$ passes to get to the last person, and another $n - 1$ passes (backwards through the line) to get back to the first person. So the cycle resets every $2(n - 1)$ passes. |
| 6 | + - Therefore we can immeidately mod the number of passes by $2(n - 1)$ without affecting the final position |
| 7 | + - After doing so, if the remainder is less than $n$, return the remainder + 1. Otherwise (the remainder must be greater than or equal to $n$ and less than $2(n - 1) - 1$ return $2n$ - the remainder - 1. |
4 | 8 |
|
5 | 9 | ## Refining the problem
|
| 10 | + - Any special cases to deal with? |
| 11 | + - If $n == 1$, should we just return 1? --> It looks like $2 \leq n \leq 1000$, so we don't need to handle this case |
| 12 | + - If time is 0, just return 1? --> Again, it looks like $1 \leq \mathrm{time} \leq 1000$, so no need to handle this case either |
6 | 13 |
|
7 | 14 | ## Attempted solution(s)
|
| 15 | + |
| 16 | +```python |
| 17 | +class Solution: |
| 18 | + def passThePillow(self, n: int, time: int) -> int: |
| 19 | + time %= 2 * (n - 1) |
| 20 | + if time < n: |
| 21 | + return time + 1 |
| 22 | + else: |
| 23 | + return 2 * n - time - 1 |
| 24 | +``` |
| 25 | +- given test cases pass |
| 26 | +- new test cases: |
| 27 | + - n = 1000, time = 1000: pass |
| 28 | + - n = 50, time = 324: pass |
| 29 | + - n = 4, time = 999: pass |
| 30 | + - n = 2, time = 1000: pass |
| 31 | +- There don't seem to be any important edge cases that I'm missing; submitting... |
| 32 | + |
| 33 | +<img width="690" alt="Screenshot 2024-07-05 at 11 11 29 PM" src="https://github.com/ContextLab/leetcode-solutions/assets/9030494/163e0790-5c4a-408d-9fca-3ce769b83c4e"> |
| 34 | + |
| 35 | +Solved! |
0 commit comments