1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (English)/找出 K 秒后拿着球的孩子(English) [find-the-child-who-has-the-ball-after-k-seconds].html
2024-06-25 01:21:44 +08:00

136 lines
3.3 KiB
HTML

<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>. There are <code>n</code> children numbered from <code>0</code> to <code>n - 1</code> standing in a queue <em>in order</em> from left to right.</p>
<p>Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches <strong>either</strong> end of the line, i.e. child 0 or child <code>n - 1</code>, the direction of passing is <strong>reversed</strong>.</p>
<p>Return the number of the child who receives the ball after <code>k</code> seconds.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<table>
<tbody>
<tr>
<th>Time elapsed</th>
<th>Children</th>
</tr>
<tr>
<td><code>0</code></td>
<td><code>[<u>0</u>, 1, 2]</code></td>
</tr>
<tr>
<td><code>1</code></td>
<td><code>[0, <u>1</u>, 2]</code></td>
</tr>
<tr>
<td><code>2</code></td>
<td><code>[0, 1, <u>2</u>]</code></td>
</tr>
<tr>
<td><code>3</code></td>
<td><code>[0, <u>1</u>, 2]</code></td>
</tr>
<tr>
<td><code>4</code></td>
<td><code>[<u>0</u>, 1, 2]</code></td>
</tr>
<tr>
<td><code>5</code></td>
<td><code>[0, <u>1</u>, 2]</code></td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<table>
<tbody>
<tr>
<th>Time elapsed</th>
<th>Children</th>
</tr>
<tr>
<td><code>0</code></td>
<td><code>[<u>0</u>, 1, 2, 3, 4]</code></td>
</tr>
<tr>
<td><code>1</code></td>
<td><code>[0, <u>1</u>, 2, 3, 4]</code></td>
</tr>
<tr>
<td><code>2</code></td>
<td><code>[0, 1, <u>2</u>, 3, 4]</code></td>
</tr>
<tr>
<td><code>3</code></td>
<td><code>[0, 1, 2, <u>3</u>, 4]</code></td>
</tr>
<tr>
<td><code>4</code></td>
<td><code>[0, 1, 2, 3, <u>4</u>]</code></td>
</tr>
<tr>
<td><code>5</code></td>
<td><code>[0, 1, 2, <u>3</u>, 4]</code></td>
</tr>
<tr>
<td><code>6</code></td>
<td><code>[0, 1, <u>2</u>, 3, 4]</code></td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<table>
<tbody>
<tr>
<th>Time elapsed</th>
<th>Children</th>
</tr>
<tr>
<td><code>0</code></td>
<td><code>[<u>0</u>, 1, 2, 3]</code></td>
</tr>
<tr>
<td><code>1</code></td>
<td><code>[0, <u>1</u>, 2, 3]</code></td>
</tr>
<tr>
<td><code>2</code></td>
<td><code>[0, 1, <u>2</u>, 3]</code></td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 50</code></li>
<li><code>1 &lt;= k &lt;= 50</code></li>
</ul>