🔻PS/Codetree

[Codetree] 행복한 수열의 개수 Python

_니지 2024. 7. 23. 14:11

https://www.codetree.ai/missions/2/problems/number-of-happy-sequence/description

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

import sys

input = sys.stdin.readline

n, m = map(int, input().split(" "))

# happy = 0
happy = []
graph = []

for _ in range(n):
    temp = list(map(int, input().rstrip().split(" ")))
    graph.append(temp)


t_graph = list(map(list, zip(*graph)))


if m == 1:
    print(2*n)
else:
    for row in graph:
        for i in range(n-m+1):
            cnt = 0
            for k in range(i, i+m-1):
                if row[k] == row[k+1]:
                    cnt += 1
            if cnt+1 == m:
                happy.append(row)
                continue   

    for row in t_graph:
        for i in range(n-m+1): #열의 범위
            cnt = 0
            for k in range(i, i+m-1): # 열의 범위 내에서 움직이기
                if row[k] == row[k+1]:
                    cnt += 1
            if cnt+1 == m:
                happy.append(row)
                continue
                
    print(len(list(set(tuple(h) for h in happy))))
728x90
반응형