2151. Maximum Good People Based on Statements (Hard) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def maximumGood(self, statements: List[List[int]]) -> int:
        n = len(statements)
        h = defaultdict(list)
        for i in range(n):
            for j in range(n):
                if statements[i][j] != 2:
                    h[i].append([j, statements[i][j]])
        for i in range(n):
            comb = list(itertools.combinations(range(n), i))
            for arr in comb:
                test = [1] * n
                for j in arr:
                    test[j] = 0
                res = 1
                for k in range(n):
                    for a, b in h[k]:
                        if test[k] == 1 and b != test[a]:
                            res = 0
                            break
                    if not res:
                        break
                if res:
                    return n - i
        return 0