Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 069cece

Browse files
committed
update 1366.rank-teams-by-votes.cpp
1 parent d59b875 commit 069cece

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

‎1366.rank-teams-by-votes.cpp‎

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,45 +101,25 @@
101101
// @lc code=start
102102
class Solution {
103103
public:
104-
string callme(string& st, unordered_map<char, vector<int>>& mm, int i){
105-
if(st.size() <= 1) return st;
106-
107-
if(i >= mm[st[0]].size()){
108-
sort(st.begin(), st.end());
109-
return st;
110-
}
111-
112-
sort(st.begin(), st.end(), [&mm,&i](char x,char y){
113-
return mm[x][i] > mm[y][i];
114-
});
115-
116-
string ret = "";
117-
int j = 0;
118-
119-
while(j<st.size()){
120-
int first = j;
121-
string tmp = "";
122-
123-
while(j<st.size() && mm[st[first]][i] == mm[st[j]][i])
124-
tmp += st[j++];
125-
126-
ret += callme(tmp, mm, i+1);
127-
}
128-
return ret;
129-
}
130-
131104
string rankTeams(vector<string>& votes) {
132-
int n = votes[0].size();
133-
105+
string st = votes[0];
106+
134107
unordered_map<char, vector<int>> mm;
135-
for(char c: votes[0])
136-
mm[c] = vector<int>(n, 0);
108+
for(char c: st)
109+
mm[c] = vector<int>(st.size(), 0);
137110

138111
for(int i=0; i<votes.size(); i++)
139-
for(int c=0; c<n; c++)
112+
for(int c=0; c<st.size(); c++)
140113
mm[votes[i][c]][c]++;
141-
142-
return callme(votes[0], mm, 0);
114+
115+
sort(st.begin(), st.end(), [&mm](const char& x, const char& y){
116+
for(int i=0; i<mm[x].size(); i++)
117+
if(mm[x][i] != mm[y][i])
118+
return mm[x][i] > mm[y][i];
119+
return x < y;
120+
});
121+
122+
return st;
143123
}
144124
};
145125
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /