-
1366. Rank Teams by Votes릿코드(LEETCODE) 2020. 3. 3. 11:33반응형
https://leetcode.com/problems/rank-teams-by-votes/
a~z 까지 알파벳의 가중치의 합과 정렬을 이용
struct Ele { char ch; vector<int> cnt; Ele() { cnt.resize(26, 0); } }; bool operator < (const Ele& a, const Ele& b) { if (a.cnt != b.cnt) return a.cnt > b.cnt; return a.ch < b.ch; } class Solution { public: string rankTeams(vector<string>& votes) { map<char, vector<int>> my; for (int i = 0; i < votes.size(); ++i) { for (int j = 0; j < votes[i].size(); ++j) { char key = votes[i][j]; if (my.count(key) == 0) { my[key] = vector<int>(26, 0); } my[key][j]++; } } vector<Ele> solArr; map<char, vector<int>>::iterator it = my.begin(); for (; it != my.end(); ++it) { char key = it->first; Ele ele; ele.ch = key; ele.cnt = it->second; solArr.push_back(ele); } sort(solArr.begin(), solArr.end()); string sol; for (int i = 0; i < solArr.size(); ++i) sol.push_back(solArr[i].ch); return sol; } };
반응형'릿코드(LEETCODE)' 카테고리의 다른 글
1207. Unique Number of Occurrences (0) 2020.03.03 1365. How Many Numbers Are Smaller Than the Current Number (0) 2020.03.03 733. Flood Fill (0) 2020.03.03 463. Island Perimeter (0) 2020.03.03 1207. Unique Number of Occurrences (0) 2020.02.28