Skip to content

Commit a3956f8

Browse files
authored
Create card-flipping-game.cpp
1 parent 7569b25 commit a3956f8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

C++/card-flipping-game.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int flipgame(vector<int>& fronts, vector<int>& backs) {
7+
unordered_set<int> same;
8+
for (int i = 0; i < fronts.size(); ++i) {
9+
if (fronts[i] == backs[i]) {
10+
same.emplace(fronts[i]);
11+
}
12+
}
13+
int result = numeric_limits<int>::max();
14+
for (const auto& n : fronts) {
15+
if (!same.count(n)) {
16+
result = min(result, n);
17+
}
18+
}
19+
for (const auto& n : backs) {
20+
if (!same.count(n)) {
21+
result = min(result, n);
22+
}
23+
}
24+
return result != numeric_limits<int>::max() ? result : 0;
25+
}
26+
};

0 commit comments

Comments
 (0)