{"id":9477,"date":"2022-02-04T18:13:31","date_gmt":"2022-02-05T02:13:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9477"},"modified":"2022-02-04T18:15:55","modified_gmt":"2022-02-05T02:15:55","slug":"leetcode-2151-maximum-good-people-based-on-statements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-2151-maximum-good-people-based-on-statements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2151. Maximum Good People Based on Statements"},"content":{"rendered":"\n<p>There are two types of persons:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<strong>good person<\/strong>: The person who always tells the truth.<\/li><li>The&nbsp;<strong>bad person<\/strong>: The person who might tell the truth and might lie.<\/li><\/ul>\n\n\n\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>statements<\/code>&nbsp;of size&nbsp;<code>n x n<\/code>&nbsp;that represents the statements made by&nbsp;<code>n<\/code>&nbsp;people about each other. More specifically,&nbsp;<code>statements[i][j]<\/code>&nbsp;could be one of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0<\/code>&nbsp;which represents a statement made by person&nbsp;<code>i<\/code>&nbsp;that person&nbsp;<code>j<\/code>&nbsp;is a&nbsp;<strong>bad<\/strong>&nbsp;person.<\/li><li><code>1<\/code>&nbsp;which represents a statement made by person&nbsp;<code>i<\/code>&nbsp;that person&nbsp;<code>j<\/code>&nbsp;is a&nbsp;<strong>good<\/strong>&nbsp;person.<\/li><li><code>2<\/code>&nbsp;represents that&nbsp;<strong>no statement<\/strong>&nbsp;is made by person&nbsp;<code>i<\/code>&nbsp;about person&nbsp;<code>j<\/code>.<\/li><\/ul>\n\n\n\n<p>Additionally, no person ever makes a statement about themselves. Formally, we have that&nbsp;<code>statements[i][i] = 2<\/code>&nbsp;for all&nbsp;<code>0 &lt;= i &lt; n<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of people who can be&nbsp;<strong>good<\/strong>&nbsp;based on the statements made by the&nbsp;<\/em><code>n<\/code><em>&nbsp;people<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/01\/15\/logic1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> statements = [[2,1,2],[1,2,2],[2,0,2]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Each person makes a single statement.\n- Person 0 states that person 1 is good.\n- Person 1 states that person 0 is good.\n- Person 2 states that person 1 is bad.\nLet's take person 2 as the key.\n- Assuming that person 2 is a good person:\n    - Based on the statement made by person 2, person 1 is a bad person.\n    - Now we know for sure that person 1 is bad and person 2 is good.\n    - Based on the statement made by person 1, and since person 1 is bad, they could be:\n        - telling the truth. There will be a contradiction in this case and this assumption is invalid.\n        - lying. In this case, person 0 is also a bad person and lied in their statement.\n    - <strong>Following that person 2 is a good person, there will be only one good person in the group<\/strong>.\n- Assuming that person 2 is a bad person:\n    - Based on the statement made by person 2, and since person 2 is bad, they could be:\n        - telling the truth. Following this scenario, person 0 and 1 are both bad as explained before.\n            - <strong>Following that person 2 is bad but told the truth, there will be no good persons in the group<\/strong>.\n        - lying. In this case person 1 is a good person.\n            - Since person 1 is a good person, person 0 is also a good person.\n            - <strong>Following that person 2 is bad and lied, there will be two good persons in the group<\/strong>.\nWe can see that at most 2 persons are good in the best case, so we return 2.\nNote that there is more than one way to arrive at this conclusion.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/01\/15\/logic2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> statements = [[2,0],[0,2]]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Each person makes a single statement.\n- Person 0 states that person 1 is bad.\n- Person 1 states that person 0 is bad.\nLet's take person 0 as the key.\n- Assuming that person 0 is a good person:\n    - Based on the statement made by person 0, person 1 is a bad person and was lying.\n    - <strong>Following that person 0 is a good person, there will be only one good person in the group<\/strong>.\n- Assuming that person 0 is a bad person:\n    - Based on the statement made by person 0, and since person 0 is bad, they could be:\n        - telling the truth. Following this scenario, person 0 and 1 are both bad.\n            - <strong>Following that person 0 is bad but told the truth, there will be no good persons in the group<\/strong>.\n        - lying. In this case person 1 is a good person.\n            - <strong>Following that person 0 is bad and lied, there will be only one good person in the group<\/strong>.\nWe can see that at most, one person is good in the best case, so we return 1.\nNote that there is more than one way to arrive at this conclusion.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == statements.length == statements[i].length<\/code><\/li><li><code>2 &lt;= n &lt;= 15<\/code><\/li><li><code>statements[i][j]<\/code>&nbsp;is either&nbsp;<code>0<\/code>,&nbsp;<code>1<\/code>, or&nbsp;<code>2<\/code>.<\/li><li><code>statements[i][i] == 2<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Combination \/ Bitmask<\/strong><\/h2>\n\n\n\n<p>Enumerate all subsets of n people and assume they are good people. Check whether their statements have any conflicts. We can ignore the statements from bad people since those can be either true or false and does not affect our checks.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>2<sup>n<\/sup>)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumGood(vector<vector<int>>& statements) {\n    const int n = statements.size();\n    auto valid = [&](int s) {\n      for (int i = 0; i < n; ++i) {\n        if (!(s >> i & 1)) continue;\n        for (int j = 0; j < n; ++j) {\n          const bool good = s >> j & 1;\n          if ((good && statements[i][j] == 0) || (!good && statements[i][j] == 1))\n            return false;\n        }\n      }\n      return true;\n    };\n    int ans = 0;\n    for (int s = 1; s < 1 << n; ++s)\n      if (valid(s)) ans = max(ans, __builtin_popcount(s));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are two types of persons: The&nbsp;good person: The person who always tells the truth. The&nbsp;bad person: The person who might tell the truth and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[621,122,217,696],"class_list":["post-9477","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bitmask","tag-combination","tag-hard","tag-subsets","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9477"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9477\/revisions"}],"predecessor-version":[{"id":9480,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9477\/revisions\/9480"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}