This problem looks busy at first, but the accepted solution is built around one steady invariant. For 794. Valid Tic-Tac-Toe State, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are countLines, intersection, validTicTacToe.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 42.52% of C++ online submissions for Valid Tic-Tac-Toe State.
02//Memory Usage: 8.4 MB, less than 91.51% of C++ online submissions for Valid Tic-Tac-Toe State.
03class Solution {
04public:
05 vector<int> countLines(vector<string>& board, char c){
06 int lines = 0;
07 /*
08 record (i,j) is passed by how many lines
09 */
10 vector<int> intersection(9, 0);
11
12 //horizontal
13 for(int i = 0; i < 3; ++i){
14 if(count(board[i].begin(), board[i].end(), c) == 3){
15 ++lines;
16 ++intersection[i*3+0];
17 ++intersection[i*3+1];
18 ++intersection[i*3+2];
19 }
20 }
21
22 //vertical
23 for(int i = 0; i < 3; ++i){
24 if(board[0][i] == c && board[1][i] == c && board[2][i] == c){
25 ++lines;
26 ++intersection[0*3+i];
27 ++intersection[1*3+i];
28 ++intersection[2*3+i];
29 }
30 }
31
32 if(board[0][0] == c && board[1][1] == c && board[2][2] == c){
33 ++lines;
34 ++intersection[0*3+0];
35 ++intersection[1*3+1];
36 ++intersection[2*3+2];
37 }
38
39 if(board[0][2] == c && board[1][1] == c && board[2][0] == c){
40 ++lines;
41 ++intersection[0*3+2];
42 ++intersection[1*3+1];
43 ++intersection[2*3+0];
44 }
45
46 return {lines, *max_element(intersection.begin(), intersection.end())};
47 };
48
49 bool validTicTacToe(vector<string>& board) {
50 int circle = 0, cross = 0;
51
52 for(string& s : board){
53 circle += count(s.begin(), s.end(), 'O');
54 cross += count(s.begin(), s.end(), 'X');
55 }
56
57 //first player puts 'X'
58 if(circle > cross) return false;
59
60 //player 1 and 2 take turns
61 if(cross >= circle+2) return false;
62
63 vector<int> crossLines = countLines(board, 'X');
64 vector<int> circleLines = countLines(board, 'O');
65
66 //both players win
67 if(crossLines[0] >= 1 && circleLines[0] >= 1) return false;
68
69 if(crossLines[0] == 2){
70 //one player get 2 lines and the game ends
71 //need to check whether the 2 lines have intersection
72 //if 'X' wins, cross-1 must equal to circle
73 return (crossLines[1] == 2) && (cross-1 == circle);
74 }else if(circleLines[0] == 2){
75 return (circleLines[1] == 2) && (cross == circle);
76 }
77
78 //only one player win
79 bool oneWin = ((crossLines[0] == 1) && (cross-1 == circle)) ||
80 (circleLines[0] == 1) && (cross == circle);
81
82 bool noWin = (crossLines[0] == 0) && (circleLines[0] == 0);
83
84 return oneWin || noWin;
85 }
86};
Cost