Class Solution

java.lang.Object
g2501_2600.s2582_pass_the_pillow.Solution

public class Solution extends Object
2582 - Pass the Pillow.<p>Easy</p> <p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. 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.</p> <ul> <li>For example, once the pillow reaches the <code>n<sup>th</sup></code> person they pass it to the <code>n - 1<sup>th</sup></code> person, then to the <code>n - 2<sup>th</sup></code> person and so on.</li> </ul> <p>Given the two positive integers <code>n</code> and <code>time</code>, return <em>the index of the person holding the pillow after</em> <code>time</code> <em>seconds</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 4, time = 5</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2. Afer five seconds, the pillow is given to the 2<sup>nd</sup> person.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 3, time = 2</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3. Afer two seconds, the pillow is given to the 3<sup>r</sup><sup>d</sup> person.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 1000</code></li> <li><code>1 <= time <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • passThePillow

      public int passThePillow(int n, int time)