Skip to content

Commit b13fd5c

Browse files
committed
O(N)
1 parent 1d03be1 commit b13fd5c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
public:
3+
string tictactoe(vector<vector<int>>& moves) {
4+
/* Read second approach directly from solution */
5+
vector<int> a(3,0);
6+
vector<int> b(3,0);
7+
int dia = 0;
8+
int anti = 0;
9+
10+
int count = 0;
11+
12+
for(int i = 0 ; i < moves.size(); i++)
13+
{
14+
if(count % 2 == 0)
15+
{
16+
a[moves[i][0]]++;
17+
b[moves[i][1]]++;
18+
if(moves[i][0] + moves[i][1] == 2)
19+
{
20+
anti++;
21+
}
22+
if(moves[i][0] == moves[i][1])
23+
{
24+
dia++;
25+
}
26+
}
27+
else
28+
{
29+
a[moves[i][0]]--;
30+
b[moves[i][1]]--;
31+
if(moves[i][0] + moves[i][1] == 2)
32+
{
33+
anti--;
34+
}
35+
if(moves[i][0] == moves[i][1])
36+
{
37+
dia--;
38+
}
39+
}
40+
41+
if(a[0] == 3 || a[1] == 3 || a[2] == 3 || dia == 3 || anti == 3 || b[0] == 3 || b[1] == 3 || b[2] == 3)
42+
return "A";
43+
else if(a[0] == -3 || a[1] == -3 || a[2] == -3 || dia == -3 || anti == -3 || b[0] == -3 || b[1] == -3 || b[2] == -3)
44+
return "B";
45+
46+
count++;
47+
}
48+
if(moves.size()<9)
49+
return "Pending";
50+
else
51+
return "Draw";
52+
53+
54+
55+
}
56+
};

0 commit comments

Comments
 (0)