Skip to content

Commit a82b1d1

Browse files
committed
feat: add solutions to lc problem: No.1366
No.1366.Rank Teams by Votes
1 parent fea1440 commit a82b1d1

File tree

6 files changed

+128
-106
lines changed

6 files changed

+128
-106
lines changed

solution/1300-1399/1366.Rank Teams by Votes/README.md

+46-36
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ C 队获得两票「排位第一」,两票「排位第二」,两票「排位
8080

8181
<!-- 这里可写通用的实现逻辑 -->
8282

83-
哈希表计数 + 自定义排序。
83+
**方法一:计数 + 自定义排序**
84+
85+
对于每个候选人,我们可以统计他在每个排位上的票数,然后根据不同的排位依次比较票数,票数相同则比较字母。
86+
87+
时间复杂度 $O(n^2 \times \log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为候选人的数量。
8488

8589
<!-- tabs:start -->
8690

@@ -91,12 +95,12 @@ C 队获得两票「排位第一」,两票「排位第二」,两票「排位
9195
```python
9296
class Solution:
9397
def rankTeams(self, votes: List[str]) -> str:
94-
d = defaultdict(lambda: [0] * len(votes[0]))
98+
n = len(votes[0])
99+
cnt = defaultdict(lambda: [0] * n)
95100
for vote in votes:
96-
for i, v in enumerate(vote):
97-
d[v][i] -= 1
98-
ans = sorted(votes[0], key=lambda x: (d[x], x))
99-
return ''.join(ans)
101+
for i, c in enumerate(vote):
102+
cnt[c][i] += 1
103+
return "".join(sorted(votes[0], key=lambda x: (cnt[x], -ord(x)), reverse=True))
100104
```
101105

102106
### **Java**
@@ -106,27 +110,31 @@ class Solution:
106110
```java
107111
class Solution {
108112
public String rankTeams(String[] votes) {
109-
Map<Character, int[]> counter = new HashMap<>();
110113
int n = votes[0].length();
111-
for (String vote : votes) {
114+
int[][] cnt = new int[26][n];
115+
for (var vote : votes) {
112116
for (int i = 0; i < n; ++i) {
113-
char v = vote.charAt(i);
114-
counter.computeIfAbsent(v, k -> new int[26])[i]++;
117+
cnt[vote.charAt(i) - 'A'][i]++;
115118
}
116119
}
117-
List<Map.Entry<Character, int[]>> t = new ArrayList<>(counter.entrySet());
118-
Collections.sort(t, (a, b) -> {
119-
int[] v1 = a.getValue();
120-
int[] v2 = b.getValue();
121-
for (int i = 0; i < 26; ++i) {
122-
if (v1[i] != v2[i]) {
123-
return v2[i] - v1[i];
120+
Character[] cs = new Character[n];
121+
for (int i = 0; i < n; ++i) {
122+
cs[i] = votes[0].charAt(i);
123+
}
124+
Arrays.sort(cs, (a, b) -> {
125+
int i = a - 'A', j = b - 'A';
126+
for (int k = 0; k < n; ++k) {
127+
int d = cnt[i][k] - cnt[j][k];
128+
if (d != 0) {
129+
return d > 0 ? -1 : 1;
124130
}
125131
}
126-
return a.getKey() - b.getKey();
132+
return a - b;
127133
});
128134
StringBuilder ans = new StringBuilder();
129-
t.forEach(e -> ans.append(e.getKey()));
135+
for (char c : cs) {
136+
ans.append(c);
137+
}
130138
return ans.toString();
131139
}
132140
}
@@ -138,18 +146,23 @@ class Solution {
138146
class Solution {
139147
public:
140148
string rankTeams(vector<string>& votes) {
141-
unordered_map<char, vector<int>> counter;
142149
int n = votes[0].size();
150+
int cnt[26][n];
151+
memset(cnt, 0, sizeof cnt);
143152
for (auto& vote : votes) {
144153
for (int i = 0; i < n; ++i) {
145-
char v = vote[i];
146-
counter[v].resize(n);
147-
++counter[v][i];
154+
cnt[vote[i] - 'A'][i]++;
148155
}
149156
}
150157
string ans = votes[0];
151-
sort(ans.begin(), ans.end(), [&](char a, char b) {
152-
return counter[a] > counter[b] || (counter[a] == counter[b] && a < b);
158+
sort(ans.begin(), ans.end(), [&](auto& a, auto& b) {
159+
int i = a - 'A', j = b - 'A';
160+
for (int k = 0; k < n; ++k) {
161+
if (cnt[i][k] != cnt[j][k]) {
162+
return cnt[i][k] > cnt[j][k];
163+
}
164+
}
165+
return a < b;
153166
});
154167
return ans;
155168
}
@@ -160,22 +173,19 @@ public:
160173
161174
```go
162175
func rankTeams(votes []string) string {
163-
n := len(votes[0])
164-
counter := make(map[byte][]int)
165-
for _, v := range votes[0] {
166-
counter[byte(v)] = make([]int, n)
167-
}
176+
cnt := [26][26]int{}
168177
for _, vote := range votes {
169-
for i, v := range vote {
170-
counter[byte(v)][i]++
178+
for i, c := range vote {
179+
cnt[c-'A'][i]++
171180
}
172181
}
173182
ans := []byte(votes[0])
174183
sort.Slice(ans, func(i, j int) bool {
175-
v1, v2 := counter[ans[i]], counter[ans[j]]
176-
for i := range v1 {
177-
if v1[i] != v2[i] {
178-
return v1[i] > v2[i]
184+
cnt1, cnt2 := cnt[ans[i]-'A'], cnt[ans[j]-'A']
185+
for k, a := range cnt1 {
186+
b := cnt2[k]
187+
if a != b {
188+
return a > b
179189
}
180190
}
181191
return ans[i] < ans[j]

solution/1300-1399/1366.Rank Teams by Votes/README_EN.md

+41-35
Original file line numberDiff line numberDiff line change
@@ -63,40 +63,44 @@ X is the winner due to the tie-breaking rule. X has the same votes as W for the
6363
```python
6464
class Solution:
6565
def rankTeams(self, votes: List[str]) -> str:
66-
d = defaultdict(lambda: [0] * len(votes[0]))
66+
n = len(votes[0])
67+
cnt = defaultdict(lambda: [0] * n)
6768
for vote in votes:
68-
for i, v in enumerate(vote):
69-
d[v][i] -= 1
70-
ans = sorted(votes[0], key=lambda x: (d[x], x))
71-
return ''.join(ans)
69+
for i, c in enumerate(vote):
70+
cnt[c][i] += 1
71+
return "".join(sorted(votes[0], key=lambda x: (cnt[x], -ord(x)), reverse=True))
7272
```
7373

7474
### **Java**
7575

7676
```java
7777
class Solution {
7878
public String rankTeams(String[] votes) {
79-
Map<Character, int[]> counter = new HashMap<>();
8079
int n = votes[0].length();
81-
for (String vote : votes) {
80+
int[][] cnt = new int[26][n];
81+
for (var vote : votes) {
8282
for (int i = 0; i < n; ++i) {
83-
char v = vote.charAt(i);
84-
counter.computeIfAbsent(v, k -> new int[26])[i]++;
83+
cnt[vote.charAt(i) - 'A'][i]++;
8584
}
8685
}
87-
List<Map.Entry<Character, int[]>> t = new ArrayList<>(counter.entrySet());
88-
Collections.sort(t, (a, b) -> {
89-
int[] v1 = a.getValue();
90-
int[] v2 = b.getValue();
91-
for (int i = 0; i < 26; ++i) {
92-
if (v1[i] != v2[i]) {
93-
return v2[i] - v1[i];
86+
Character[] cs = new Character[n];
87+
for (int i = 0; i < n; ++i) {
88+
cs[i] = votes[0].charAt(i);
89+
}
90+
Arrays.sort(cs, (a, b) -> {
91+
int i = a - 'A', j = b - 'A';
92+
for (int k = 0; k < n; ++k) {
93+
int d = cnt[i][k] - cnt[j][k];
94+
if (d != 0) {
95+
return d > 0 ? -1 : 1;
9496
}
9597
}
96-
return a.getKey() - b.getKey();
98+
return a - b;
9799
});
98100
StringBuilder ans = new StringBuilder();
99-
t.forEach(e -> ans.append(e.getKey()));
101+
for (char c : cs) {
102+
ans.append(c);
103+
}
100104
return ans.toString();
101105
}
102106
}
@@ -108,18 +112,23 @@ class Solution {
108112
class Solution {
109113
public:
110114
string rankTeams(vector<string>& votes) {
111-
unordered_map<char, vector<int>> counter;
112115
int n = votes[0].size();
116+
int cnt[26][n];
117+
memset(cnt, 0, sizeof cnt);
113118
for (auto& vote : votes) {
114119
for (int i = 0; i < n; ++i) {
115-
char v = vote[i];
116-
counter[v].resize(n);
117-
++counter[v][i];
120+
cnt[vote[i] - 'A'][i]++;
118121
}
119122
}
120123
string ans = votes[0];
121-
sort(ans.begin(), ans.end(), [&](char a, char b) {
122-
return counter[a] > counter[b] || (counter[a] == counter[b] && a < b);
124+
sort(ans.begin(), ans.end(), [&](auto& a, auto& b) {
125+
int i = a - 'A', j = b - 'A';
126+
for (int k = 0; k < n; ++k) {
127+
if (cnt[i][k] != cnt[j][k]) {
128+
return cnt[i][k] > cnt[j][k];
129+
}
130+
}
131+
return a < b;
123132
});
124133
return ans;
125134
}
@@ -130,22 +139,19 @@ public:
130139
131140
```go
132141
func rankTeams(votes []string) string {
133-
n := len(votes[0])
134-
counter := make(map[byte][]int)
135-
for _, v := range votes[0] {
136-
counter[byte(v)] = make([]int, n)
137-
}
142+
cnt := [26][26]int{}
138143
for _, vote := range votes {
139-
for i, v := range vote {
140-
counter[byte(v)][i]++
144+
for i, c := range vote {
145+
cnt[c-'A'][i]++
141146
}
142147
}
143148
ans := []byte(votes[0])
144149
sort.Slice(ans, func(i, j int) bool {
145-
v1, v2 := counter[ans[i]], counter[ans[j]]
146-
for i := range v1 {
147-
if v1[i] != v2[i] {
148-
return v1[i] > v2[i]
150+
cnt1, cnt2 := cnt[ans[i]-'A'], cnt[ans[j]-'A']
151+
for k, a := range cnt1 {
152+
b := cnt2[k]
153+
if a != b {
154+
return a > b
149155
}
150156
}
151157
return ans[i] < ans[j]

solution/1300-1399/1366.Rank Teams by Votes/Solution.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
class Solution {
22
public:
33
string rankTeams(vector<string>& votes) {
4-
unordered_map<char, vector<int>> counter;
54
int n = votes[0].size();
5+
int cnt[26][n];
6+
memset(cnt, 0, sizeof cnt);
67
for (auto& vote : votes) {
78
for (int i = 0; i < n; ++i) {
8-
char v = vote[i];
9-
counter[v].resize(n);
10-
++counter[v][i];
9+
cnt[vote[i] - 'A'][i]++;
1110
}
1211
}
1312
string ans = votes[0];
14-
sort(ans.begin(), ans.end(), [&](char a, char b) {
15-
return counter[a] > counter[b] || (counter[a] == counter[b] && a < b);
13+
sort(ans.begin(), ans.end(), [&](auto& a, auto& b) {
14+
int i = a - 'A', j = b - 'A';
15+
for (int k = 0; k < n; ++k) {
16+
if (cnt[i][k] != cnt[j][k]) {
17+
return cnt[i][k] > cnt[j][k];
18+
}
19+
}
20+
return a < b;
1621
});
1722
return ans;
1823
}

solution/1300-1399/1366.Rank Teams by Votes/Solution.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
func rankTeams(votes []string) string {
2-
n := len(votes[0])
3-
counter := make(map[byte][]int)
4-
for _, v := range votes[0] {
5-
counter[byte(v)] = make([]int, n)
6-
}
2+
cnt := [26][26]int{}
73
for _, vote := range votes {
8-
for i, v := range vote {
9-
counter[byte(v)][i]++
4+
for i, c := range vote {
5+
cnt[c-'A'][i]++
106
}
117
}
128
ans := []byte(votes[0])
139
sort.Slice(ans, func(i, j int) bool {
14-
v1, v2 := counter[ans[i]], counter[ans[j]]
15-
for i := range v1 {
16-
if v1[i] != v2[i] {
17-
return v1[i] > v2[i]
10+
cnt1, cnt2 := cnt[ans[i]-'A'], cnt[ans[j]-'A']
11+
for k, a := range cnt1 {
12+
b := cnt2[k]
13+
if a != b {
14+
return a > b
1815
}
1916
}
2017
return ans[i] < ans[j]
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
class Solution {
22
public String rankTeams(String[] votes) {
3-
Map<Character, int[]> counter = new HashMap<>();
43
int n = votes[0].length();
5-
for (String vote : votes) {
4+
int[][] cnt = new int[26][n];
5+
for (var vote : votes) {
66
for (int i = 0; i < n; ++i) {
7-
char v = vote.charAt(i);
8-
counter.computeIfAbsent(v, k -> new int[26])[i]++;
7+
cnt[vote.charAt(i) - 'A'][i]++;
98
}
109
}
11-
List<Map.Entry<Character, int[]>> t = new ArrayList<>(counter.entrySet());
12-
Collections.sort(t, (a, b) -> {
13-
int[] v1 = a.getValue();
14-
int[] v2 = b.getValue();
15-
for (int i = 0; i < 26; ++i) {
16-
if (v1[i] != v2[i]) {
17-
return v2[i] - v1[i];
10+
Character[] cs = new Character[n];
11+
for (int i = 0; i < n; ++i) {
12+
cs[i] = votes[0].charAt(i);
13+
}
14+
Arrays.sort(cs, (a, b) -> {
15+
int i = a - 'A', j = b - 'A';
16+
for (int k = 0; k < n; ++k) {
17+
int d = cnt[i][k] - cnt[j][k];
18+
if (d != 0) {
19+
return d > 0 ? -1 : 1;
1820
}
1921
}
20-
return a.getKey() - b.getKey();
22+
return a - b;
2123
});
2224
StringBuilder ans = new StringBuilder();
23-
t.forEach(e -> ans.append(e.getKey()));
25+
for (char c : cs) {
26+
ans.append(c);
27+
}
2428
return ans.toString();
2529
}
2630
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def rankTeams(self, votes: List[str]) -> str:
3-
d = defaultdict(lambda: [0] * len(votes[0]))
3+
n = len(votes[0])
4+
cnt = defaultdict(lambda: [0] * n)
45
for vote in votes:
5-
for i, v in enumerate(vote):
6-
d[v][i] -= 1
7-
ans = sorted(votes[0], key=lambda x: (d[x], x))
8-
return ''.join(ans)
6+
for i, c in enumerate(vote):
7+
cnt[c][i] += 1
8+
return "".join(sorted(votes[0], key=lambda x: (cnt[x], -ord(x)), reverse=True))

0 commit comments

Comments
 (0)