forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess-the-word.cpp
135 lines (125 loc) · 3.9 KB
/
guess-the-word.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Time: O(n^2)
// Space: O(n)
/**
* // This is the Master's API interface.
* // You should not implement it, or speculate about its implementation
* class Master {
* public:
* int guess(string word);
* };
*/
class Solution {
public:
void findSecretWord(vector<string>& wordlist, Master& master) {
vector<vector<int>> H(wordlist.size(), vector<int>(wordlist.size()));
for (int i = 0; i < wordlist.size(); ++i) {
for (int j = 0; j < wordlist.size(); ++j) {
H[i][j] = match(wordlist[i], wordlist[j]);
}
}
vector<int> possible(wordlist.size());
iota(possible.begin(), possible.end(), 0);
int n = 0;
while (n < 6) {
auto guess = solve(H, possible);
n = master.guess(wordlist[guess]);
vector<int> new_possible;
for (const auto& j : possible) {
if (H[guess][j] == n) {
new_possible.emplace_back(j);
}
}
possible = new_possible;
}
}
private:
int solve(const vector<vector<int>>& H,
const vector<int>& possible) {
vector<int> min_max_group = possible;
int best_guess = -1;
for (const auto& guess : possible) {
vector<vector<int>> groups(7);
for (const auto& j : possible) {
if (j != guess) {
groups[H[guess][j]].emplace_back(j);
}
}
int max_group_i = 0;
for (int i = 0; i < groups.size(); ++i) {
if (groups[i].size() > groups[max_group_i].size()) {
max_group_i = i;
}
}
if (groups[max_group_i].size() < min_max_group.size()) {
min_max_group = groups[max_group_i];
best_guess = guess;
}
}
return best_guess;
}
int match(const string& a, const string& b) {
int matches = 0;
for (int i = 0; i < a.length(); ++i) {
if (a[i] == b[i]) {
++matches;
}
}
return matches;
}
};
// Time: O(n^2)
// Space: O(n)
class Solution2 {
public:
void findSecretWord(vector<string>& wordlist, Master& master) {
vector<vector<int>> H(wordlist.size(), vector<int>(wordlist.size()));
for (int i = 0; i < wordlist.size(); ++i) {
for (int j = 0; j < wordlist.size(); ++j) {
H[i][j] = match(wordlist[i], wordlist[j]);
}
}
vector<int> possible(wordlist.size());
iota(possible.begin(), possible.end(), 0);
int n = 0;
while (n < 6) {
auto guess = solve(H, possible);
n = master.guess(wordlist[guess]);
vector<int> new_possible;
for (const auto& j : possible) {
if (H[guess][j] == n) {
new_possible.emplace_back(j);
}
}
possible = new_possible;
}
}
private:
int solve(const vector<vector<int>>& H,
const vector<int>& possible) {
vector<int> min_max_group = possible;
int best_guess = -1;
for (const auto& guess : possible) {
vector<vector<int>> groups(7);
for (const auto& j : possible) {
if (j != guess) {
groups[H[guess][j]].emplace_back(j);
}
}
int max_group_i = 0;
if (groups[max_group_i].size() < min_max_group.size()) {
min_max_group = groups[max_group_i];
best_guess = guess;
}
}
return best_guess;
}
int match(const string& a, const string& b) {
int matches = 0;
for (int i = 0; i < a.length(); ++i) {
if (a[i] == b[i]) {
++matches;
}
}
return matches;
}
};