Skip to content

Commit

Permalink
add 1366
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Mar 2, 2020
1 parent 1bef623 commit 1bdac9e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,5 @@ LeetCode
|1361|[Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes/)|c|[c++](./src/1361-Validate-Binary-Tree-Nodes/1361.cpp)|[python](./src/1361-Validate-Binary-Tree-Nodes/1361.py)|[go](./src/1361-Validate-Binary-Tree-Nodes/1361.go)|[js](./src/1361-Validate-Binary-Tree-Nodes/1361.js)|[java](./src/1361-Validate-Binary-Tree-Nodes/1361.java)|Medium|
|1362|[Closest Divisors](https://leetcode.com/problems/closest-divisors/)|c|[c++](./src/1362-Closest-Divisors/1362.cpp)|[python](./src/1362-Closest-Divisors/1362.py)|[go](./src/1362-Closest-Divisors/1362.go)|[js](./src/1362-Closest-Divisors/1362.js)|[java](./src/1362-Closest-Divisors/1362.java)|Medium|
|1363|[Largest Multiple of Three](https://leetcode.com/problems/largest-multiple-of-three/)|c|[c++](./src/1363-Largest-Multiple-of-Three/1363.cpp)|[python](./src/1363-Largest-Multiple-of-Three/1363.py)|[go](./src/1363-Largest-Multiple-of-Three/1363.go)|[js](./src/1363-Largest-Multiple-of-Three/1363.js)|[java](./src/1363-Largest-Multiple-of-Three/1363.java)|Hard|
|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|c|[c++](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.cpp)|[python](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.py)|[go](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.go)|[js](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.js)|[java](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.java)|Easy|
|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|c|[c++](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.cpp)|[python](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.py)|[go](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.go)|[js](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.js)|[java](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.java)|Easy|
|1366|[Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/)|c|[c++](./src/1366-Rank-Teams-by-Votes/1366.cpp)|[python](./src/1366-Rank-Teams-by-Votes/1366.py)|[go](./src/1366-Rank-Teams-by-Votes/1366.go)|[js](./src/1366-Rank-Teams-by-Votes/1366.js)|[java](./src/1366-Rank-Teams-by-Votes/1366.java)|Medium|
23 changes: 23 additions & 0 deletions src/1366-Rank-Teams-by-Votes/1366.cpp
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];
}
};
26 changes: 26 additions & 0 deletions src/1366-Rank-Teams-by-Votes/1366.go
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)
}
21 changes: 21 additions & 0 deletions src/1366-Rank-Teams-by-Votes/1366.java
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();
}
}
20 changes: 20 additions & 0 deletions src/1366-Rank-Teams-by-Votes/1366.js
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('');
};
7 changes: 7 additions & 0 deletions src/1366-Rank-Teams-by-Votes/1366.py
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]))
8 changes: 4 additions & 4 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os, bisect

# 题目名称
name = "How Many Numbers Are Smaller Than the Current Number"
ID = 1365
url = "https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/"
difficult = "Easy"
name = "Rank Teams by Votes"
ID = 1366
url = "https://leetcode.com/problems/rank-teams-by-votes/"
difficult = "Medium"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']


Expand Down

0 comments on commit 1bdac9e

Please sign in to comment.