A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1275. Find Winner on a Tic Tac Toe Game, the solution in this repository is mainly a greedy solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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: greedy.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are isWin, tictactoe, a_row.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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: 8 ms, faster than 5.13% of C++ online submissions for Find Winner on a Tic Tac Toe Game.
02//Memory Usage: 9.4 MB, less than 100.00% of C++ online submissions for Find Winner on a Tic Tac Toe Game.
03
04class Solution {
05public:
06 bool isWin(set<vector<int>>& one_moves){
07 set<vector<int>> line;
08 vector<vector<int>> vec(max(3, (int)one_moves.size()));
09 vector<vector<int>>::iterator v_it;
10
11 // sort(one_moves.begin(), one_moves.end());
12
13 //horizontal line
14 for(int i = 0; i < 3; i++){
15 line = {{i, 0}, {i, 1}, {i, 2}};
16
17 v_it = set_intersection(one_moves.begin(), one_moves.end(), line.begin(), line.end(), vec.begin());
18 if(v_it-vec.begin() == 3) return true;
19 }
20
21 //vertical line
22 for(int j = 0; j < 3; j++){
23 line = {{0,j},{1,j},{2,j}};
24 v_it = set_intersection(one_moves.begin(), one_moves.end(), line.begin(), line.end(), vec.begin());
25 if(v_it-vec.begin() == 3) return true;
26 }
27
28 //diagonal
29 line = {{0,0},{1,1},{2,2}};
30 v_it = set_intersection(one_moves.begin(), one_moves.end(), line.begin(), line.end(), vec.begin());
31 if(v_it-vec.begin() == 3) return true;
32
33 line = {{0,2},{1,1},{2,0}};
34 v_it = set_intersection(one_moves.begin(), one_moves.end(), line.begin(), line.end(), vec.begin());
35 if(v_it-vec.begin() == 3) return true;
36
37 return false;
38 }
39
40 string tictactoe(vector<vector<int>>& moves) {
41 set<vector<int>> a_moves, b_moves;
42 for(int i = 0; i < moves.size(); i++){
43 if(i%2 == 0){
44 a_moves.insert(moves[i]);
45 }else{
46 b_moves.insert(moves[i]);
47 }
48 }
49
50 if(isWin(a_moves)) return "A";
51 if(isWin(b_moves)) return "B";
52 return moves.size() == 9 ? "Draw" : "Pending";
53 }
54};
55
56//https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/441319/JavaPython-3-Check-rows-columns-and-two-diagonals-w-brief-explanation-and-analysis.
57//Runtime: 4 ms, faster than 59.83% of C++ online submissions for Find Winner on a Tic Tac Toe Game.
58//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Find Winner on a Tic Tac Toe Game.
59
60class Solution {
61public:
62 string tictactoe(vector<vector<int>>& moves) {
63 vector<int> a_row(3), a_col(3), b_row(3), b_col(3);
64 int aD1 = 0, aD2 = 0, bD1 = 0, bD2 = 0;
65 for(int i = 0; i < moves.size(); i++){
66 vector<int> move = moves[i];
67 int r = move[0], c = move[1];
68 if(i % 2 == 0){
69 //A
70 if(++a_row[r] == 3 || ++a_col[c] == 3 ||
71 r == c && ++aD1 == 3 || r + c == 2 && ++aD2 == 3) return "A";
72 }else{
73 //B
74 if(++b_row[r] == 3 || ++b_col[c] == 3 ||
75 r == c && ++bD1 == 3 || r + c == 2 && ++bD2 == 3) return "B";
76 }
77 }
78 return moves.size() == 9 ? "Draw" : "Pending";
79 }
80};
Cost