|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../delivering-boxes-from-storage-to-ports "Delivering Boxes from Storage to Ports") |
| 9 | + |
| 10 | +[Next >](../partitioning-into-minimum-number-of-deci-binary-numbers "Partitioning Into Minimum Number Of Deci-Binary Numbers") |
| 11 | + |
| 12 | +## [1688. Count of Matches in Tournament (Easy)](https://leetcode.com/problems/count-of-matches-in-tournament "比赛中的配对次数") |
| 13 | + |
| 14 | +<p>You are given an integer <code>n</code>, the number of teams in a tournament that has strange rules:</p> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li>If the current number of teams is <strong>even</strong>, each team gets paired with another team. A total of <code>n / 2</code> matches are played, and <code>n / 2</code> teams advance to the next round.</li> |
| 18 | + <li>If the current number of teams is <strong>odd</strong>, one team randomly advances in the tournament, and the rest gets paired. A total of <code>(n - 1) / 2</code> matches are played, and <code>(n - 1) / 2 + 1</code> teams advance to the next round.</li> |
| 19 | +</ul> |
| 20 | + |
| 21 | +<p>Return <em>the number of matches played in the tournament until a winner is decided.</em></p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong>Example 1:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> n = 7 |
| 28 | +<strong>Output:</strong> 6 |
| 29 | +<strong>Explanation:</strong> Details of the tournament: |
| 30 | +- 1st Round: Teams = 7, Matches = 3, and 4 teams advance. |
| 31 | +- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance. |
| 32 | +- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner. |
| 33 | +Total number of matches = 3 + 2 + 1 = 6. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>Example 2:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input:</strong> n = 14 |
| 40 | +<strong>Output:</strong> 13 |
| 41 | +<strong>Explanation:</strong> Details of the tournament: |
| 42 | +- 1st Round: Teams = 14, Matches = 7, and 7 teams advance. |
| 43 | +- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance. |
| 44 | +- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance. |
| 45 | +- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner. |
| 46 | +Total number of matches = 7 + 3 + 2 + 1 = 13. |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= n <= 200</code></li> |
| 54 | +</ul> |
| 55 | + |
| 56 | +### Related Topics |
| 57 | + [[Backtracking](../../tag/backtracking/README.md)] |
| 58 | + |
| 59 | +### Hints |
| 60 | +<details> |
| 61 | +<summary>Hint 1</summary> |
| 62 | +Simulate the tournament as given in the statement. |
| 63 | +</details> |
| 64 | + |
| 65 | +<details> |
| 66 | +<summary>Hint 2</summary> |
| 67 | +Be careful when handling odd integers. |
| 68 | +</details> |
0 commit comments