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 _themaximum number of people who can be good based on the statements made by the _npeople.

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

Input: statements =[[2,0],[0,2]]Output: 1Explanation: Each person makes a single statement.- Person 0 states that person 1is bad.- Person 1 states that person 0is bad.Let's take person 0 as the key.- Assuming that person 0is a good person:- Based on the statement made by person 0, person 1is a bad person and was lying.-**Following that person 0is a good person, there will be only one good person in the group**.- Assuming that person 0is a bad person:- Based on the statement made by person 0, and since person 0is bad, they could be:- telling the truth. Following this scenario, person 0 and 1 are both bad.-**Following that person 0is bad but told the truth, there will be no good persons in the group**.- lying. In thiscase person 1is a good person.-**Following that person 0is bad and lied, there will be only one good person in the group**.We can see that at most, one person is good in the best case, so we return1.Note that there is more than one way to arrive at this conclusion.
Since the number of people n is small (≤15), we can try every possible combination of who is good and who is bad using bitmasking. For each combination, we check if it is valid: every good person must tell the truth, so their statements must match the assignment. We maximize the number of good people among all valid assignments.
classSolution {
funmaximumGood(statements: Array<IntArray>): Int {
val n = statements.size
var ans = 0for (mask in0 until (1 shl n)) {
var valid = truefor (i in0 until n) {
if ((mask shr i) and 1==0) continuefor (j in0 until n) {
if (statements[i][j] ==2) continueif (statements[i][j] != ((mask shr j) and 1)) {
valid = falsebreak }
}
if (!valid) break }
if (valid) ans = maxOf(ans, mask.countOneBits())
}
return ans
}
}