Maximum Good People Based on Statements

There are two types of persons:

  • The good person: The person who always tells the truth.
  • The bad person: The person who might tell the truth and might lie.

You are given a 0-indexed 2D integer array statements of size n x n that represents the statements made by n people about each other. More specifically, statements[i][j] could be one of the following:

  • 0 which represents a statement made by person i that person j is a bad person.
  • 1 which represents a statement made by person i that person j is a good person.
  • 2 represents that no statement is made by person i about person j.

Additionally, no person ever makes a statement about themselves. Formally, we have that statements[i][i] = 2 for all 0 <= i < n.

Return the maximum number of people who can be good based on the statements made by the n people.

Example 1:

Input: statements = [[2,1,2],[1,2,2],[2,0,2]]
Output: 2
Explanation: Each person makes a single statement.
- Person 0 states that person 1 is good.
- Person 1 states that person 0 is good.
- Person 2 states that person 1 is bad.
Let's take person 2 as the key.
- Assuming that person 2 is a good person:
    - Based on the statement made by person 2, person 1 is a bad person.
    - Now we know for sure that person 1 is bad and person 2 is good.
    - Based on the statement made by person 1, and since person 1 is bad, they could be:
        - telling the truth. There will be a contradiction in this case and this assumption is invalid.
        - lying. In this case, person 0 is also a bad person and lied in their statement.
    - Following that person 2 is a good person, there will be only one good person in the group.
- Assuming that person 2 is a bad person:
    - Based on the statement made by person 2, and since person 2 is bad, they could be:
        - telling the truth. Following this scenario, person 0 and 1 are both bad as explained before.
            - Following that person 2 is bad but told the truth, there will be no good persons in the group.
        - lying. In this case person 1 is a good person.
            - Since person 1 is a good person, person 0 is also a good person.
            - Following that person 2 is bad and lied, there will be two good persons in the group.
We can see that at most 2 persons are good in the best case, so we return 2.
Note that there is more than one way to arrive at this conclusion.

思路:这题正着做,很难,但是可以变成一个判定问题,就是generate all possiblities,然后根据谁是1,他的话,也就是这一行 statement[i][j] 里面,跟generate出来的,是否contracdict来判断。然后count出max的可能性;

class Solution {
    public int maximumGood(int[][] statements) {
        StringBuilder sb = new StringBuilder();
        int[] res = new int[]{0};
        dfs(sb, statements, 0, 0, res);
        return res[0];
    }
    
    private void dfs(StringBuilder sb, int[][] statements, int index, int count, int[] res) {
        if(index == statements.length) {
            if(isvalid(statements, sb)) {
                res[0] = Math.max(res[0], count);
            }
            return;
        }

        // bad person;
        sb.append("0");
        dfs(sb, statements, index + 1, count, res);
        sb.deleteCharAt(sb.length() - 1);
        
        // good person;
        sb.append("1");
        dfs(sb, statements, index + 1, count + 1, res);
        sb.deleteCharAt(sb.length() - 1);
    }
    
    private boolean isvalid(int[][] statements, StringBuilder sb) {
        String str = sb.toString();
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == '1') {
                for(int j = 0; j < statements.length; j++) {
                    if(statements[i][j] != 2 && statements[i][j] != str.charAt(j) - '0') {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值