1 Star 0 Fork 18

zcc/力扣题库(完整版)

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
find-the-child-who-has-the-ball-after-k-seconds.html 3.26 KB
Copy Edit Raw Blame History
zhangbk1 authored 2024-06-25 09:35 +08:00 . update
<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>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tutuzhang0/leetcode-problemset.git
git@gitee.com:tutuzhang0/leetcode-problemset.git
tutuzhang0
leetcode-problemset
力扣题库(完整版)
master

Search