Maximum Good People Based on Statements - Complete Solution Guide
Maximum Good People Based on Statements is LeetCode problem 2151, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i
Detailed Explanation
The problem asks us to find the maximum number of 'good' people possible given a set of statements made by 'n' people about each other. Each person is either 'good' (always tells the truth) or 'bad' (might lie). The statements are represented in an n x n matrix, where statements[i][j] can be 0 (person i says person j is bad), 1 (person i says person j is good), or 2 (person i makes no statement about person j). We need to determine an assignment of 'good' and 'bad' to each person such that the statements made by the 'good' people are consistent with the assignments. The goal is to maximize the number of 'good' people in a consistent assignment.
Solution Approach
The solution employs a brute-force approach using bit manipulation. It iterates through all possible assignments of 'good' and 'bad' to the 'n' people. Each assignment is represented by a bitmask of length 'n'. For each assignment, it checks if the statements made by the people assumed to be 'good' are consistent with the assumed assignment itself. If an assignment is consistent, it counts the number of 'good' people and updates the maximum count found so far.
Step-by-Step Algorithm
- Step 1: Iterate through all possible bitmasks from 0 to 2^n - 1. Each bitmask represents a possible assignment of 'good' and 'bad' to the people.
- Step 2: For each bitmask, check for consistency. Iterate through all people from 0 to n-1.
- Step 3: If the i-th person is assumed to be 'good' (i-th bit in the mask is set), iterate through all people from 0 to n-1 again.
- Step 4: If the i-th person made a statement about the j-th person (statements[i][j] is not 2), check if the statement is consistent with the assignment in the mask. Specifically, check if statements[i][j] is equal to the j-th bit in the mask.
- Step 5: If any statement made by a 'good' person is inconsistent, mark the assignment as inconsistent and break the inner loop.
- Step 6: If the assignment is consistent after checking all statements, count the number of 'good' people in the assignment.
- Step 7: Update the maximum number of 'good' people found so far.
- Step 8: Return the maximum number of 'good' people.
Key Insights
- Insight 1: The core idea is to try all possible combinations of 'good' and 'bad' assignments to the people and check if the statements made by the 'good' people are consistent with the assumed assignments.
- Insight 2: Bit manipulation is a natural fit for representing the assignments, as each bit in a mask can represent whether a person is 'good' or 'bad'.
- Insight 3: The problem constraints (n <= 15) suggest an exponential time complexity solution (like trying all possible assignments) is acceptable.
Complexity Analysis
Time Complexity: O(n*2^n*n)
Space Complexity: O(1)
Topics
This problem involves: Array, Backtracking, Bit Manipulation, Enumeration.
Companies
Asked at: TuSimple.