| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.FindWinnerOnTicTacToeGame; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindWinnerOnTicTacToeGameSimulation : IFindWinnerOnTicTacToeGame |
| | 16 | | { |
| | 17 | | private const int GridSize = 3; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(1) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="moves"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public string Tictactoe(int[][] moves) |
| 3 | 26 | | { |
| 3 | 27 | | var rowSum = new int[GridSize]; |
| 3 | 28 | | var columnSum = new int[GridSize]; |
| 3 | 29 | | var leftDiagonalSum = 0; |
| 3 | 30 | | var rightDiagonalSum = 0; |
| | 31 | |
|
| 42 | 32 | | for (var i = 0; i < moves.Length; i++) |
| 20 | 33 | | { |
| 20 | 34 | | var value = i % 2 == 0 ? 1 : -1; |
| | 35 | |
|
| 20 | 36 | | var row = moves[i][0]; |
| 20 | 37 | | var col = moves[i][1]; |
| | 38 | |
|
| 20 | 39 | | rowSum[row] += value; |
| 20 | 40 | | columnSum[col] += value; |
| | 41 | |
|
| 20 | 42 | | if (row == col) |
| 8 | 43 | | { |
| 8 | 44 | | leftDiagonalSum += value; |
| 8 | 45 | | } |
| | 46 | |
|
| 20 | 47 | | if (row + col == GridSize - 1) |
| 8 | 48 | | { |
| 8 | 49 | | rightDiagonalSum += value; |
| 8 | 50 | | } |
| | 51 | |
|
| 20 | 52 | | if (Math.Abs(rowSum[row]) == GridSize || |
| 20 | 53 | | Math.Abs(columnSum[col]) == GridSize || |
| 20 | 54 | | Math.Abs(leftDiagonalSum) == GridSize || |
| 20 | 55 | | Math.Abs(rightDiagonalSum) == GridSize) |
| 2 | 56 | | { |
| 2 | 57 | | return value == 1 ? "A" : "B"; |
| | 58 | | } |
| 18 | 59 | | } |
| | 60 | |
|
| 1 | 61 | | return moves.Length == GridSize * GridSize ? "Draw" : "Pending"; |
| 3 | 62 | | } |
| | 63 | | } |