{"id":7799,"date":"2020-12-13T12:15:36","date_gmt":"2020-12-13T20:15:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7799"},"modified":"2020-12-13T12:17:02","modified_gmt":"2020-12-13T20:17:02","slug":"leetcode-1688-count-of-matches-in-tournament","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1688-count-of-matches-in-tournament\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1688. Count of Matches in Tournament"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>n<\/code>, the number of teams in a tournament that has strange rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the current number of teams is&nbsp;<strong>even<\/strong>, each team gets paired with another team. A total of&nbsp;<code>n \/ 2<\/code>&nbsp;matches are played, and&nbsp;<code>n \/ 2<\/code>&nbsp;teams advance to the next round.<\/li><li>If the current number of teams is&nbsp;<strong>odd<\/strong>, one team randomly advances in the tournament, and the rest gets paired. A total of&nbsp;<code>(n - 1) \/ 2<\/code>&nbsp;matches are played, and&nbsp;<code>(n - 1) \/ 2 + 1<\/code>&nbsp;teams advance to the next round.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of matches played in the tournament until a winner is decided.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> Details of the tournament: \n- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.\n- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.\n- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.\nTotal number of matches = 3 + 2 + 1 = 6.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 14\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong> Details of the tournament:\n- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.\n- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.\n- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.\n- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner.\nTotal number of matches = 7 + 3 + 2 + 1 = 13.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 200<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation \/ Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Ver 1\nclass Solution {\npublic:\n  int numberOfMatches(int n) {\n    int ans = 0;\n    while (n &gt; 1) {\n      ans += n \/ 2 + (n & 1);\n      n \/= 2;\n    }\n    return ans;\n  }\n};\n\/\/ Ver 2\nclass Solution {\npublic:\n  int numberOfMatches(int n) {\n    return n &gt; 1 ? n \/ 2 + (n & 1) + numberOfMatches(n \/ 2) : 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer&nbsp;n, the number of teams in a tournament that has strange rules: If the current number of teams is&nbsp;even, each team&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153,48],"tags":[222,459,17,179],"class_list":["post-7799","post","type-post","status-publish","format-standard","hentry","category-recursion","category-simulation","tag-easy","tag-ologn","tag-recursion","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7799"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7799\/revisions"}],"predecessor-version":[{"id":7802,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7799\/revisions\/7802"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}