Leetcode # 1275. Find Winner on a Tic Tac Toe Game

https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game

Solution

Time Complexity: O(len(moves))
Space Complexity: O(len(row))
(The input and output generally do not count towards the space complexity.)

class Solution:
  def tictactoe(self, moves: List[List[int]]) -> str:
    row = [0] * 3
    col = [0] * 3
    dia = [0] * 2
    
    player = 1
    for y, x in moves:
      row[y] += player
      col[x] += player
      dia[0] += player if x == y else 0
      dia[1] += player if x + y == 2 else 0

      if any(abs(line) == 3 for line in [row[y], col[x]] + dia):
        return "A" if player == 1 else "B"
      
      player *= -1
    return "Draw" if len(moves) == 9 else "Pending"

 

Last Updated on 2023/08/16 by A1go

目錄

目錄
Bitnami