forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bef623
commit 1bdac9e
Showing
7 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution | ||
{ | ||
public: | ||
string rankTeams(vector<string>& votes) | ||
{ | ||
int n = votes[0].size(); | ||
vector<vector<int>> cnt(26, vector<int>(n + 1, 0)); | ||
for (int i = 0; i < 26; i++) cnt[i][n] = i; | ||
|
||
for (const string& vote : votes) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cnt[vote[i] - 'A'][i]--; | ||
} | ||
} | ||
|
||
sort(votes[0].begin(), votes[0].end(), [&](const char a, const char b) { | ||
return cnt[a - 'A'] < cnt[b - 'A']; | ||
}); | ||
return votes[0]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
func rankTeams(votes []string) string { | ||
n := len(votes[0]) | ||
cnt := make([][]int, 26) | ||
for i := 0; i < 26; i++ { | ||
cnt[i] = make([]int, n + 1) | ||
cnt[i][n] = i | ||
} | ||
|
||
for _, vote := range votes { | ||
for i := 0; i < n; i++ { | ||
cnt[vote[i] - 'A'][i]-- | ||
} | ||
} | ||
|
||
res := []byte(votes[0]) | ||
sort.Slice(res, func(a, b int) bool { | ||
for i := 0; i <= n; i++ { | ||
if cnt[res[a] - 'A'][i] == cnt[res[b] - 'A'][i] { | ||
continue | ||
} | ||
return cnt[res[a] - 'A'][i] < cnt[res[b] - 'A'][i] | ||
} | ||
return true | ||
}) | ||
return string(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public String rankTeams(String[] votes) { | ||
int n = votes[0].length(); | ||
int[][] cnt = new int[26][n + 1]; | ||
for (int i = 0; i < 26; i++) cnt[i][n] = i; | ||
|
||
for (String vote : votes) { | ||
for (int i = 0; i < n; i++) { | ||
cnt[vote.charAt(i) - 'A'][i]--; | ||
} | ||
} | ||
|
||
Character[] res = new Character[n]; | ||
for (int i = 0; i < n; i++) res[i] = votes[0].charAt(i); | ||
Arrays.sort(res, (a, b) -> Arrays.compare(cnt[a - 'A'], cnt[b - 'A'])); // Java 9 | ||
|
||
StringBuilder ss = new StringBuilder(); | ||
for (char s : res) ss.append(s); | ||
return ss.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var rankTeams = function(votes) { | ||
let n = votes[0].length, cnt = []; | ||
for (let i = 0; i < 26; i++) { | ||
cnt[i] = new Array(n + 1).fill(0); | ||
cnt[i][n] = i; | ||
} | ||
|
||
for (let vote of votes) { | ||
for (let i = 0; i < n; i++) { | ||
cnt[vote.charCodeAt(i) - 65][i]--; | ||
} | ||
} | ||
return votes[0].split('').sort((a, b) => { | ||
for (let i = 0; i <= n; i++) { | ||
if (cnt[a.charCodeAt() - 65][i] == cnt[b.charCodeAt() - 65][i]) continue; | ||
return cnt[a.charCodeAt() - 65][i] - cnt[b.charCodeAt() - 65][i]; | ||
} | ||
return 0; | ||
}).join(''); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution: | ||
def rankTeams(self, votes: List[str]) -> str: | ||
cnt = {v: [0] * len(votes[0]) + [v] for v in votes[0]} | ||
for vote in votes: | ||
for i, v in enumerate(vote): | ||
cnt[v][i] -= 1 | ||
return "".join(sorted(votes[0], key=lambda x: cnt[x])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters