세용용용용 2024. 7. 15. 11:59

 

코딩테스트 연습 - 혼자서 하는 틱택토 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

나의 풀이

# 2024-07-15
def solution(board):
    answer = 1
    o_ct = 0
    x_ct = 0
    o_win = 0
    x_win = 0
    
    # 가로 승리조건, (O,X) 카운트
    for row in board:
        #print(row)
        now_o_ct = row.count('O')
        now_x_ct = row.count('X')
    
        o_ct+=now_o_ct
        x_ct+=now_x_ct
        if now_o_ct==3:
            o_win+=1
        elif now_x_ct==3:
            x_win+=1
        #print(row)
    # 세로 승리조건, 대각선 승리조건
    for col in range(len(board)):
        test_set = set()
        dia_set = set()

        for i in range(len(board)):
            test_set.add(board[i][col])
            if col==0:
                dia_set.add(board[i][col+i])
            elif col==2:
                dia_set.add(board[i][col-i])
        #print(test_set, dia_set)
        if len(test_set)==1:
            for now_set in test_set:
                if now_set=='O':
                    o_win+=1
                elif now_set=='X':
                    x_win+=1
        if len(dia_set)==1:
            for dia in dia_set:
                if dia=='O':
                    o_win+=1
                elif dia=='X':
                    x_win+=1
                    
    # 카운트로 추론
    if (o_ct-x_ct<0) or (o_ct-x_ct>1):
        return 0

    # 끝나야되는디 안끝나는거
    if o_win>0 and x_win>0:
        return 0
    elif o_win>=1 and (o_ct<=x_ct):
        return 0
    elif x_win>=1 and (x_ct!=o_ct):
        return 0
    
    #print(o_ct, x_ct, o_win, x_win)
    return answer

 

시간 복잡도

입력 board 크기는 고정
즉, 입력 크기에 따라 실행시간이 달라지지 않으므로 
시간복잡도는 상수 시간 ( O(1) )