mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 15:31:43 +08:00
37 lines
1.8 KiB
HTML
37 lines
1.8 KiB
HTML
<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> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> n = 4, time = 5
|
|
<strong>Output:</strong> 2
|
|
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
|
|
After five seconds, the 2<sup>nd</sup> person is holding the pillow.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> n = 3, time = 2
|
|
<strong>Output:</strong> 3
|
|
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3.
|
|
After two seconds, the 3<sup>r</sup><sup>d</sup> person is holding the pillow.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>2 <= n <= 1000</code></li>
|
|
<li><code>1 <= time <= 1000</code></li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds/description/" target="_blank"> 3178: Find the Child Who Has the Ball After K Seconds.</a></p>
|