Leetcode Problem 2582. Pass the Pillow

2582. Pass the Pillow

Leetcode Solutions

Optimized Pillow Passing Simulation

  1. Calculate the number of complete cycles by dividing time by 2 * (n - 1).
  2. Calculate the remaining time after the complete cycles by taking the modulus of time with 2 * (n - 1).
  3. If the remaining time is less than n, the pillow is being passed in the forward direction. The position of the pillow is the same as the remaining time.
  4. If the remaining time is greater than or equal to n, the pillow is being passed in the reverse direction. The position of the pillow is 2 * n - remaining time - 1.
  5. Return the calculated position of the pillow.
UML Thumbnail

Iterative Pillow Passing Simulation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...