<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>subsets Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/subsets/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/subsets/</link>
	<description></description>
	<lastBuildDate>Sat, 05 Feb 2022 02:15:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>subsets Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/subsets/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2151. Maximum Good People Based on Statements</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-2151-maximum-good-people-based-on-statements/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-2151-maximum-good-people-based-on-statements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Feb 2022 02:13:31 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[bitmask]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[subsets]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9477</guid>

					<description><![CDATA[<p>There are two types of persons: The&#160;good person: The person who always tells the truth. The&#160;bad person: The person who might tell the truth and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-2151-maximum-good-people-based-on-statements/">花花酱 LeetCode 2151. Maximum Good People Based on Statements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>There are two types of persons:</p>



<ul><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>



<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>



<ul><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>



<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>



<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>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/01/15/logic1.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> statements = [[2,1,2],[1,2,2],[2,0,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 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.
    - <strong>Following that person 2 is a good person, there will be only one good person in the group</strong>.
- 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.
            - <strong>Following that person 2 is bad but told the truth, there will be no good persons in the group</strong>.
        - lying. In this case person 1 is a good person.
            - Since person 1 is a good person, person 0 is also a good person.
            - <strong>Following that person 2 is bad and lied, there will be two good persons in the group</strong>.
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.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/01/15/logic2.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> statements = [[2,0],[0,2]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Each person makes a single statement.
- Person 0 states that person 1 is bad.
- Person 1 states that person 0 is bad.
Let's take person 0 as the key.
- Assuming that person 0 is a good person:
    - Based on the statement made by person 0, person 1 is a bad person and was lying.
    - <strong>Following that person 0 is a good person, there will be only one good person in the group</strong>.
- Assuming that person 0 is a bad person:
    - Based on the statement made by person 0, and since person 0 is bad, they could be:
        - telling the truth. Following this scenario, person 0 and 1 are both bad.
            - <strong>Following that person 0 is bad but told the truth, there will be no good persons in the group</strong>.
        - lying. In this case person 1 is a good person.
            - <strong>Following that person 0 is bad and lied, there will be only one good person in the group</strong>.
We can see that at most, one person is good in the best case, so we return 1.
Note that there is more than one way to arrive at this conclusion.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Combination / Bitmask</strong></h2>



<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>



<p>Time complexity: O(n<sup>2</sup>2<sup>n</sup>)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maximumGood(vector&lt;vector&lt;int&gt;&gt;&amp; statements) {
    const int n = statements.size();
    auto valid = [&amp;](int s) {
      for (int i = 0; i &lt; n; ++i) {
        if (!(s &gt;&gt; i &amp; 1)) continue;
        for (int j = 0; j &lt; n; ++j) {
          const bool good = s &gt;&gt; j &amp; 1;
          if ((good &amp;&amp; statements[i][j] == 0) || (!good &amp;&amp; statements[i][j] == 1))
            return false;
        }
      }
      return true;
    };
    int ans = 0;
    for (int s = 1; s &lt; 1 &lt;&lt; n; ++s)
      if (valid(s)) ans = max(ans, __builtin_popcount(s));
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-2151-maximum-good-people-based-on-statements/">花花酱 LeetCode 2151. Maximum Good People Based on Statements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/searching/leetcode-2151-maximum-good-people-based-on-statements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1755. Closest Subsequence Sum</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1755-closest-subsequence-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1755-closest-subsequence-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 13 Feb 2021 17:08:29 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[subsets]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8095</guid>

					<description><![CDATA[<p>You are given an integer array nums and an integer goal. You want to choose a subsequence of&#160;nums&#160;such that the sum of its elements is the closest possible&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1755-closest-subsequence-sum/">花花酱 LeetCode 1755. Closest Subsequence Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1755. Closest Subsequence Sum - 刷题找工作 EP382" width="500" height="281" src="https://www.youtube.com/embed/CiTqABg6oaw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an integer array <code>nums</code> and an integer <code>goal</code>.</p>



<p>You want to choose a subsequence of&nbsp;<code>nums</code>&nbsp;such that the sum of its elements is the closest possible to&nbsp;<code>goal</code>. That is, if the sum of the subsequence&#8217;s elements is&nbsp;<code>sum</code>, then you want to&nbsp;<strong>minimize the absolute difference</strong>&nbsp;<code>abs(sum - goal)</code>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;possible value of</em>&nbsp;<code>abs(sum - goal)</code>.</p>



<p>Note that a subsequence of an array is an array formed by removing some elements&nbsp;<strong>(possibly all or none)</strong>&nbsp;of the original array.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,-7,3,5], goal = 6
<strong>Output:</strong> 0
<strong>Explanation:</strong> Choose the whole array as a subsequence, with a sum of 6.
This is equal to the goal, so the absolute difference is 0.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [7,-9,15,-2], goal = -5
<strong>Output:</strong> 1
<strong>Explanation:</strong> Choose the subsequence [7,-9,-2], with a sum of -4.
The absolute difference is abs(-4 - (-5)) = abs(1) = 1, which is the minimum.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3], goal = -7
<strong>Output:</strong> 7
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 40</code></li><li><code>-10<sup>7</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>7</sup></code></li><li><code>-10<sup>9</sup>&nbsp;&lt;= goal &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Binary Search</strong></h2>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-1.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-1.png" alt="" class="wp-image-8098" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-2.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-2.png" alt="" class="wp-image-8099" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1755-ep382-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>Since n is too large to generate sums for all subsets O(2^n), we have to split the array into half, generate two sum sets. O(2^(n/2)).</p>



<p>Then the problem can be reduced to find the closet sum by picking one number (sum) each from two different arrays which can be solved in O(mlogm), where m = 2^(n/2).</p>



<p>So final time complexity is O(n * 2^(n/2))<br>Space complexity: O(2^(n/2))</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minAbsDifference(vector&lt;int&gt;&amp; nums, int goal) {
    const int n = nums.size();
    int ans = abs(goal);
    vector&lt;int&gt; t1{0}, t2{0};
    t1.reserve(1 &lt;&lt; (n / 2 + 1));
    t2.reserve(1 &lt;&lt; (n / 2 + 1));
    for (int i = 0; i &lt; n / 2; ++i)
      for (int j = t1.size() - 1; j &gt;= 0; --j)
        t1.push_back(t1[j] + nums[i]);
    for (int i = n / 2; i &lt; n; ++i)
      for (int j = t2.size() - 1; j &gt;= 0; --j)
        t2.push_back(t2[j] + nums[i]);
    set&lt;int&gt; s2(begin(t2), end(t2));
    for (int x : unordered_set&lt;int&gt;(begin(t1), end(t1))) {
      auto it = s2.lower_bound(goal - x);
      if (it != s2.end())
        ans = min(ans, abs(goal - x - *it));
      if (it != s2.begin())
        ans = min(ans, abs(goal - x - *prev(it)));
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1755-closest-subsequence-sum/">花花酱 LeetCode 1755. Closest Subsequence Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1755-closest-subsequence-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
