알고리즘/프로그래머스

쿼드압축 후 개수 세기

happenundo 2024. 5. 13. 21:22
728x90
 

프로그래머스

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

programmers.co.kr


 

# 해당하는 영역이 모두 같은 수로 이루어져 있는지 확인하는 함수
def check(arr):
    n = len(arr)
    global zero_cnt
    global one_cnt
    
    total = 0
    for row in arr:
        total += sum(row)
        
    
    if n == 1:
        if arr[0][0] == 0:
            zero_cnt += 1
        else:
            one_cnt += 1
        return
    if total == 0:
        zero_cnt += 1
        return
    elif total == n * n:
        one_cnt += 1
        return
    else:
        temp = []
        for i in range(n//2):
            temp.append(arr[:n//2][i][:n//2])    
        check(temp)
        
        temp = []
        for i in range(n//2):
            temp.append(arr[:n//2][i][n//2:])
        check(temp)
        
        temp = []
        for i in range(n//2):
            temp.append(arr[n//2:][i][n//2:])
        check(temp)
        
        temp = []
        for i in range(n//2):
            temp.append(arr[n//2:][i][:n//2])
        check(temp)
        

one_cnt = 0
zero_cnt = 0

def solution(arr):
    answer = []
    global one_cnt
    global zero_cnt
    
    
    check(arr)

    return [zero_cnt, one_cnt]

 

상남자식 재귀 풀이로 풀었다.

728x90