@@ -80,7 +80,11 @@ C 队获得两票「排位第一」,两票「排位第二」,两票「排位
80
80
81
81
<!-- 这里可写通用的实现逻辑 -->
82
82
83
- 哈希表计数 + 自定义排序。
83
+ ** 方法一:计数 + 自定义排序**
84
+
85
+ 对于每个候选人,我们可以统计他在每个排位上的票数,然后根据不同的排位依次比较票数,票数相同则比较字母。
86
+
87
+ 时间复杂度 $O(n^2 \times \log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为候选人的数量。
84
88
85
89
<!-- tabs:start -->
86
90
@@ -91,12 +95,12 @@ C 队获得两票「排位第一」,两票「排位第二」,两票「排位
91
95
``` python
92
96
class Solution :
93
97
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)
95
100
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 ))
100
104
```
101
105
102
106
### ** Java**
@@ -106,27 +110,31 @@ class Solution:
106
110
``` java
107
111
class Solution {
108
112
public String rankTeams (String [] votes ) {
109
- Map<Character , int[]> counter = new HashMap<> ();
110
113
int n = votes[0 ]. length();
111
- for (String vote : votes) {
114
+ int [][] cnt = new int [26 ][n];
115
+ for (var vote : votes) {
112
116
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]++ ;
115
118
}
116
119
}
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 ;
124
130
}
125
131
}
126
- return a. getKey() - b. getKey() ;
132
+ return a - b;
127
133
});
128
134
StringBuilder ans = new StringBuilder ();
129
- t. forEach(e - > ans. append(e. getKey()));
135
+ for (char c : cs) {
136
+ ans. append(c);
137
+ }
130
138
return ans. toString();
131
139
}
132
140
}
@@ -138,18 +146,23 @@ class Solution {
138
146
class Solution {
139
147
public:
140
148
string rankTeams(vector<string >& votes) {
141
- unordered_map<char, vector<int >> counter;
142
149
int n = votes[ 0] .size();
150
+ int cnt[ 26] [ n ] ;
151
+ memset(cnt, 0, sizeof cnt);
143
152
for (auto& vote : votes) {
144
153
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 ] ++;
148
155
}
149
156
}
150
157
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;
153
166
});
154
167
return ans;
155
168
}
@@ -160,22 +173,19 @@ public:
160
173
161
174
```go
162
175
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{}
168
177
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]++
171
180
}
172
181
}
173
182
ans := []byte(votes[0])
174
183
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
179
189
}
180
190
}
181
191
return ans[i] < ans[j]
0 commit comments