-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtwo-letter-card-game.cpp
More file actions
31 lines (30 loc) · 1.03 KB
/
two-letter-card-game.cpp
File metadata and controls
31 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Time: O(n + 26)
// Space: O(26)
// freq table, math
class Solution {
public:
int score(vector<string>& cards, char x) {
vector<int> cnt1(10), cnt2(10);
int cnt3 = 0;
for (const auto& s : cards) {
if (s.find(x) == string::npos) {
continue;
}
if (s[0] == x && s[1] == x) {
++cnt3;
} else if (s[0] == x) {
++cnt1[s[1] - 'a'];
} else if (s[1] == x) {
++cnt2[s[0] - 'a'];
}
}
const int total1 = accumulate(cbegin(cnt1), cend(cnt1), 0);
const int total2 = accumulate(cbegin(cnt2), cend(cnt2), 0);
const int mx1 = ranges::max(cnt1);
const int mx2 = ranges::max(cnt2);
const int pair1 = min(total1 - mx1, total1 / 2);
const int pair2 = min(total2 - mx2, total2 / 2);
const int pair3 = min((total1 - 2 * pair1) + (total2 - 2 * pair2), cnt3);
return (pair1 + pair2) + pair3 + min(pair1 + pair2, (cnt3 - pair3) / 2);
}
};