Ctrl K

How Many Witnesses Can Be Honest

Hard
There are n witnesses. Each one is either **honest**, meaning they always tell the truth, or **dishonest**, meaning they may tell the truth or lie. The witnesses have spoken about one another. You are given an n × n matrix claims: - claims[i][j] = 1 means witness i says j is honest; - claims[i][j] = 0 means witness i says j is dishonest; - claims[i][j] = 2 means witness i said nothing about j. Nobody speaks about themselves: claims[i][i] = 2. Return the largest number of witnesses that can be honest without contradicting what the honest ones said.
Example 1
claims=[[2, 1, 0], [2, 2, 2], [2, 2, 2]] → 2

All three cannot be honest, since 0 would then be truthfully calling 2 a liar. If 0 is honest, 0 and 1 can be honest; if not, 1 and 2 can. Three is impossible, so the answer is 2.

Example 2
claims=[[2, 0], [0, 2]] → 1

Each one calls the other a liar, so they cannot both be honest, but either one alone can.

Example 3
claims=[[2, 2], [2, 2]] → 2
Constraints
  • 2 ≤ n = len(claims) ≤ 15
  • len(claims[i]) = n
  • claims[i][j] is 0, 1 or 2
  • claims[i][i] = 2
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMoves Between Offices HardXOR of Every Subset EasyPicks With the Full OR MediumOutshoot the Rival MediumColumns That Cover Rows Medium
loading Python…
Input
claims=[[2, 1, 0], [2, 2, 2], [2, 2, 2]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 74 tests on the server, hidden ones included.