#2582

Pass the Pillow

pupil · 400 · lc easy +24 · verified · 56.6% accepted · 1,100 likes · top 51%

Description

n people labeled 1 to n stand in a line. Person 1 starts with a pillow. Each second the pillow moves one step forward; when it reaches person n the direction reverses. Given n and time, return the label of the person holding the pillow after time seconds.

Example 1:

Input: n = 4, time = 5
Output: 2
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
After five seconds, the 2nd person is holding the pillow.

Example 2:

Input: n = 3, time = 2
Output: 3
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
After two seconds, the 3rd person is holding the pillow.

Code

1
2
3