{"id":6392,"date":"2020-03-02T00:31:51","date_gmt":"2020-03-02T08:31:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6392"},"modified":"2020-03-02T00:36:51","modified_gmt":"2020-03-02T08:36:51","slug":"leetcode-1366-rank-teams-by-votes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1366-rank-teams-by-votes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1366. Rank Teams by Votes"},"content":{"rendered":"\n<p>In a special ranking system,&nbsp;each voter gives a rank from highest to lowest to all teams participated in the competition.<\/p>\n\n\n\n<p>The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second position to resolve the conflict, if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, we rank them alphabetically based on their team letter.<\/p>\n\n\n\n<p>Given an array of strings&nbsp;<code>votes<\/code>&nbsp;which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.<\/p>\n\n\n\n<p>Return&nbsp;<em>a string of all teams<\/em>&nbsp;<strong>sorted<\/strong>&nbsp;by the ranking system.<\/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> votes = [\"ABC\",\"ACB\",\"ABC\",\"ACB\",\"ACB\"]\n<strong>Output:<\/strong> \"ACB\"\n<strong>Explanation:<\/strong> Team A was ranked first place by 5 voters. No other team was voted as first place so team A is the first team.\nTeam B was ranked second by 2 voters and was ranked third by 3 voters.\nTeam C was ranked second by 3 voters and was ranked third by 2 voters.\nAs most of the voters ranked C second, team C is the second team and team B is the third.\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> votes = [\"WXYZ\",\"XYZW\"]\n<strong>Output:<\/strong> \"XWYZ\"\n<strong>Explanation:<\/strong> X is the winner due to tie-breaking rule. X has same votes as W for the first position but X has one vote as second position while W doesn't have any votes as second position. \n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> votes = [\"ZMNAGUEDSJYLBOPHRQICWFXTVK\"]\n<strong>Output:<\/strong> \"ZMNAGUEDSJYLBOPHRQICWFXTVK\"\n<strong>Explanation:<\/strong> Only one voter so his votes are used for the ranking.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> votes = [\"BCA\",\"CAB\",\"CBA\",\"ABC\",\"ACB\",\"BAC\"]\n<strong>Output:<\/strong> \"ABC\"\n<strong>Explanation:<\/strong> \nTeam A was ranked first by 2 voters, second by 2 voters and third by 2 voters.\nTeam B was ranked first by 2 voters, second by 2 voters and third by 2 voters.\nTeam C was ranked first by 2 voters, second by 2 voters and third by 2 voters.\nThere is a tie and we rank teams ascending by their IDs.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> votes = [\"M\",\"M\",\"M\",\"M\"]\n<strong>Output:<\/strong> \"M\"\n<strong>Explanation:<\/strong> Only team M in the competition so it has the first rank.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= votes.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= votes[i].length &lt;= 26<\/code><\/li><li><code>votes[i].length ==&nbsp;votes[j].length<\/code>&nbsp;for&nbsp;<code>0 &lt;= i, j &lt; votes.length<\/code>.<\/li><li><code>votes[i][j]<\/code>&nbsp;is an English&nbsp;<strong>upper-case<\/strong>&nbsp;letter.<\/li><li>All characters of&nbsp;<code>votes[i]<\/code>&nbsp;are unique.<\/li><li>All the characters&nbsp;that occur&nbsp;in&nbsp;<code>votes[0]<\/code>&nbsp;<strong>also&nbsp;occur<\/strong>&nbsp;in&nbsp;<code>votes[j]<\/code>&nbsp;where&nbsp;<code>1 &lt;= j &lt; votes.length<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sort by rank<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(v*n + n^2*logn)<br>Space complexity: O(n*n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string rankTeams(vector<string>& votes) {\n    const int n = votes[0].length();\n    string ans(votes[0]);\n    vector<vector<int>> rank(26, vector<int>(n));\n    \n    for (const auto& vote : votes)\n      for (int i = 0; i < n; ++i)\n        ++rank[vote[i] - 'A'][i];\n    \n    sort(begin(ans), end(ans), [&#038;](const char i, const char j) {\n      if (rank[i - 'A'] != rank[j - 'A']) return rank[i - 'A'] > rank[j - 'A'];\n      return i < j;\n    });    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a special ranking system,&nbsp;each voter gives a rank from highest to lowest to all teams participated in the competition. The ordering of teams is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,440,179,15],"class_list":["post-6392","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-rank","tag-simulation","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6392","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=6392"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6392\/revisions"}],"predecessor-version":[{"id":6394,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6392\/revisions\/6394"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}