[Mock] Adobe 4

shsh·2021년 7월 15일
0

Mock

목록 보기
84/93

1275. Find Winner on a Tic Tac Toe Game

Tic-tac-toe is played by two players A and B on a 3 x 3 grid.

Here are the rules of Tic-Tac-Toe:

  • Players take turns placing characters into empty squares (" ").
  • The first player A always places "X" characters, while the second player B always places "O" characters.
  • "X" and "O" characters are always placed into empty squares, never on filled ones.
  • The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
  • The game also ends if all squares are non-empty.
  • No more moves can be played if the game is over.

Given an array moves where each element is another array of size 2 corresponding to the row and column of the grid where they mark their respective character in the order in which A and B play.

Return the winner of the game if it exists (A or B), in case the game ends in a draw return "Draw", if there are still movements to play return "Pending".

You can assume that moves is valid (It follows the rules of Tic-Tac-Toe), the grid is initially empty and A will play first.

My Answer 1: Accepted (Runtime: 24 ms - 98.20% / Memory Usage: 14.1 MB - 89.81%)

class Solution:
    def tictactoe(self, moves: List[List[int]]) -> str:
        AB = 0  # A == 0, B == 1
        # row, column, diagonal
        rcd = [[[0,0], [0,1], [0,2]], [[1,0], [1,1], [1,2]], [[2,0], [2,1], [2,2]],
               [[0,0], [1,0], [2,0]], [[0,1], [1,1], [2,1]], [[0,2], [1,2], [2,2]],
               [[0,0], [1,1], [2,2]], [[0,2], [1,1], [2,0]]]
        
        A = []
        B = []
        
        for i in range(len(moves)):
            if AB == 0:
                A.append(moves[i])
            else:
                B.append(moves[i])
            AB = not AB
        
        for i in range(len(rcd)):
            cntA = 0
            cntB = 0
            for xy in rcd[i]:
                if xy in A:
                    cntA += 1
                if xy in B:
                    cntB += 1
            if cntA == 3:
                return "A"
            if cntB == 3:
                return "B"
        
        if len(moves) == 9:
            return "Draw"
        
        return "Pending"

파이썬 문자열은 인덱스 값으로 바로 수정할 수 없기 때문에... (str[i] = "a" X)

["X", "O", "X"] 식으로 리스트에 저장한 후
가로, 세로, 대각선 모두 같은지 확인하려 했지만 너무 코드가 길어져서...

3 * 3 이니까 그냥 좌표들을 rcd 에 미리 저장해서 확인함
누가 빙고 되는지 확인해서 winner return

winner 가 없을 경우, moves 가 9 번 꽉 차면 return Draw / 아니면 return Pending


1344. Angle Between Hands of a Clock

Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.

My Answer 1: Accepted (Runtime: 32 ms - 57.96% / Memory Usage: 14.2 MB - 71.25%)

class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        # 시침은 1 분마다 0.5 도씩 / 분침은 1 분마다 6 도씩
        
        h = ((hour % 12) * 30) + (minutes * 0.5)
        m = minutes * 6
        
        return abs(m - h) if 360 - abs(m - h) > abs(m - h) else 360 - abs(m - h)

12 시를 기준으로 시침과 분침의 각도를 구해서 서로 빼주기

더 작은 각도를 return 해야하므로 360 도에서 뺀 값과 비교해서 작은 값 return

profile
Hello, World!

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN