<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [LeetCode] 1366. Rank Teams by Votes 通過投票對團隊排名


      In a special ranking system, each voter gives a rank from highest to lowest to all teams participating in the competition.

      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.

      You are given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.

      Return a string of all teams sorted by the ranking system.

      Example 1:

      Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
      Output: "ACB"
      Explanation:
      Team A was ranked first place by 5 voters. No other team was voted as first place, so team A is the first team.
      Team B was ranked second by 2 voters and ranked third by 3 voters.
      Team C was ranked second by 3 voters and ranked third by 2 voters.
      As most of the voters ranked C second, team C is the second team, and team B is the third.
      

      Example 2:

      Input: votes = ["WXYZ","XYZW"]
      Output: "XWYZ"
      Explanation:
      X is the winner due to the tie-breaking rule. X has the same votes as W for the first position, but X has one vote in the second position, while W does not have any votes in the second position.
      

      Example 3:

      Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
      Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
      Explanation: Only one voter, so their votes are used for the ranking.
      

      Constraints:

      • 1 <= votes.length <= 1000
      • 1 <= votes[i].length <= 26
      • votes[i].length == votes[j].length for 0 <= i, j < votes.length.
      • votes[i][j] is an English uppercase letter.
      • All characters of votes[i] are unique.
      • All the characters that occur in votes[0] also occur in votes[j] where 1 <= j < votes.length.

      這道題給了一個排名系統,每張選票把不同的候選者進行排名,最終對候選者進行投票統計的方法是,首先數其排在第一位的票數,若得票最多,就在最終排名上放在第一,若有兩個候選者排在第一位的票數相同,則比較第二位的票數,并以此類推進行排名,若排最后還是沒有分出勝負,則按名字的字母順序進行排序。題意并不是很難理解,這里的候選者的名字就用大寫的單個字母進行代替了,每張選票就是就是一個字符串,且長度相等。由于候選者出現的位置很重要,要按不同位置來統計每個候選者的得票數,題目中限定了最多只有 26 個候選者,那么排位就有 26 個,每個候選者都需要一個長度為 26 的數組來統計每個排位上的得票數。

      這里用個小 trick,由于題目中所有若各個排名都相同的話,則使用候選者的名字進行排序,所以這里用 27 個位置,最后一個位置放候選人的名字,排序起來非常方便。這里就建立一個大小為 26 by 27 的二維數組 cnt,其中 cnt[i][j] 表示候選者i在第j個位置上的得票數,當然 cnt[i][26] 除外,是候選者字母的 ASCII 碼值。由于并不是每次都有 26 個候選者,所以只需要把存在的候選者的名字加入到第 27 個位置中,可以遍歷第一張選票的候選者,直接加入字符的 ASCII 碼即可。接下來就是統計票數了,遍歷每一張選票,對于每一張遍歷到的選票,遍歷每一個位置,將該位置上的值減去1,這里為啥不是對應票數加1呢,這里實際上也是用了一個小 trick,因為排序是默認從小到大,而我們想讓得票數高的排在前面,就可以用負數。唱票完成后,就可以直接組成想要返回的字符串即可,參見代碼如下:


      class Solution {
      public:
          string rankTeams(vector<string>& votes) {
              string res;
              vector<vector<int>> cnt(26, vector<int>(27));
              for (char c : votes[0]) {
                  cnt[c - 'A'][26] = c;
              }
              for (string vote : votes) {
                  for (int i = 0; i < vote.size(); ++i) {
                      --cnt[vote[i] - 'A'][i];
                  }
              }
              sort(cnt.begin(), cnt.end());
              for (int i = 0; i < votes[0].size(); ++i) {
                  res += cnt[i][26];
              }
              return res;
          }
      };
      

      Github 同步地址:

      https://github.com/grandyang/leetcode/issues/1366


      類似題目:

      Online Election


      參考資料:

      https://leetcode.com/problems/rank-teams-by-votes

      https://leetcode.com/problems/online-election/solutions/173382/c-java-python-binary-search-in-times/


      LeetCode All in One 題目講解匯總(持續更新中...)

      posted @ 2024-07-06 22:53  Grandyang  閱讀(159)  評論(0)    收藏  舉報
      Fork me on GitHub
      主站蜘蛛池模板: 国产精品一区二区三区日韩 | 亚洲精品专区永久免费区| 国产成人午夜福利在线播放| 99RE6在线观看国产精品| 亚洲欧美中文字幕日韩一区二区| 日本免费一区二区三区日本| 噜噜噜亚洲色成人网站∨| 性欧美VIDEOFREE高清大喷水| 久久精品国产一区二区三区| 99久久无码私人网站| 白嫩少妇bbw撒尿视频| 国产自拍偷拍视频在线观看| 成人午夜激情在线观看| 日本少妇被黑人xxxxx| 国产精品va在线观看h| 麻豆一区二区三区蜜桃免费| 国产精品久久久久久无毒不卡| 日韩av日韩av在线| 无码a∨高潮抽搐流白浆| 天美传媒mv免费观看完整| 欧美成年黄网站色视频| 国产精品二区中文字幕| 亚洲精品国精品久久99热| 国产精品国产精品一区精品| 国产青榴视频在线观看| 国产成人啪精品午夜网站| 日韩人妻av一区二区三区| 蜜臀午夜一区二区在线播放| 色噜噜在线视频免费观看| 久久这里有精品国产电影网| 方正县| 蜜臀av一区二区精品字幕| 国偷自产一区二区三区在线视频 | 亚洲国产成人资源在线| 久久久精品94久久精品| 国产精品老熟女露脸视频| 久久午夜无码鲁丝片直播午夜精品 | 久久婷婷成人综合色| 激情伊人五月天久久综合| 亚洲一级片一区二区三区| 国产精品国产高清国产av|