<?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>group Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/group/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/group/</link>
	<description></description>
	<lastBuildDate>Sun, 06 Sep 2020 05:19:08 +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>group Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/group/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1578. Minimum Deletion Cost to Avoid Repeating Letters</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1578-minimum-deletion-cost-to-avoid-repeating-letters/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1578-minimum-deletion-cost-to-avoid-repeating-letters/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Sep 2020 05:18:44 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7351</guid>

					<description><![CDATA[<p>Given a&#160;string&#160;s&#160;and an array of integers&#160;cost&#160;where&#160;cost[i]&#160;is the cost of&#160;deleting&#160;the character&#160;i&#160;in&#160;s. Return the minimum cost of deletions&#160;such that there are no two identical letters next to&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1578-minimum-deletion-cost-to-avoid-repeating-letters/">花花酱 LeetCode 1578. Minimum Deletion Cost to Avoid Repeating Letters</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>Given a&nbsp;string&nbsp;<code>s</code>&nbsp;and an array of integers&nbsp;<code>cost</code>&nbsp;where&nbsp;<code>cost[i]</code>&nbsp;is the cost of&nbsp;deleting&nbsp;the character&nbsp;<code>i</code>&nbsp;in&nbsp;<code>s</code>.</p>



<p>Return the minimum cost of deletions&nbsp;such that there are no two identical letters next to each other.</p>



<p>Notice that you will delete the chosen characters at the same time, in other words, after deleting a character, the costs of deleting&nbsp;other characters will not change.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "abaac", cost = [1,2,3,4,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Delete the letter "a" with cost 3 to get "abac" (String without two identical letters next to each other).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "abc", cost = [1,2,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> You don't need to delete any character because there are no identical letters next to each other.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aabaa", cost = [1,2,3,4,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Delete the first and the last character, getting the string ("aba").
</pre>



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



<ul><li><code>s.length == cost.length</code></li><li><code>1 &lt;= s.length, cost.length &lt;= 10^5</code></li><li><code>1 &lt;= cost[i] &lt;=&nbsp;10^4</code></li><li><code>s</code>&nbsp;contains only lowercase English letters.</li></ul>



<h2><strong>Solution: Group by group</strong></h2>



<p>For a group of same letters, delete all expect the one with the highest cost.</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  int minCost(string s, vector&lt;int&gt;&amp; cost) {
    int t = cost[0];
    int m = cost[0];
    int ans = 0;
    for (int i = 1; i &lt; s.length(); ++i) {
      if (s[i] != s[i - 1]) {
        ans += t - m;
        t = m = 0;
      }
      t += cost[i];
      m = max(m, cost[i]);
    }
    return ans + (t - m);
  }
};</pre>

</div><h2 class="tabtitle">python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def minCost(self, s: str, cost: List[int]) -&gt; int:
    s = '*' + s + '*'
    cost = [0] + cost + [0]
    ans = t = m = 0    
    for i in range(1, len(s)):
      if s[i] != s[i - 1]:
        ans += t - m
        t = m = 0
      t += cost[i]
      m = max(m, cost[i])
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1578-minimum-deletion-cost-to-avoid-repeating-letters/">花花酱 LeetCode 1578. Minimum Deletion Cost to Avoid Repeating Letters</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/string/leetcode-1578-minimum-deletion-cost-to-avoid-repeating-letters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1282. Group the People Given the Group Size They Belong To</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1282-group-the-people-given-the-group-size-they-belong-to/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1282-group-the-people-given-the-group-size-they-belong-to/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 08 Dec 2019 10:00:16 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5939</guid>

					<description><![CDATA[<p>There are&#160;n&#160;people whose&#160;IDs&#160;go from&#160;0&#160;to&#160;n - 1&#160;and each person belongs&#160;exactly&#160;to one&#160;group. Given the array&#160;groupSizes&#160;of length&#160;n&#160;telling the group size each person belongs to, return the groups there&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1282-group-the-people-given-the-group-size-they-belong-to/">花花酱 LeetCode 1282. Group the People Given the Group Size They Belong To</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&nbsp;<code>n</code>&nbsp;people whose&nbsp;<strong>IDs</strong>&nbsp;go from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n - 1</code>&nbsp;and each person belongs&nbsp;<strong>exactly</strong>&nbsp;to one&nbsp;group. Given the array&nbsp;<code>groupSizes</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;telling the group size each person belongs to, return the groups there are and the people&#8217;s&nbsp;<strong>IDs</strong>&nbsp;each group includes.</p>



<p>You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution.&nbsp;</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> groupSizes = [3,3,3,3,3,1,3]
<strong>Output:</strong> [[5],[0,1,2],[3,4,6]]
<strong>Explanation:</strong> 
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> groupSizes = [2,1,3,3,3,2]
<strong>Output:</strong> [[1],[0,5],[2,3,4]]
</pre>



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



<ul><li><code>groupSizes.length == n</code></li><li><code>1 &lt;= n&nbsp;&lt;= 500</code></li><li><code>1 &lt;=&nbsp;groupSizes[i] &lt;= n</code></li></ul>



<h2><strong>Solution: HashMap + Greedy</strong></h2>



<p>hashmap: group_size -&gt; {ids}<br>greedy: whenever a group of size s has s people, assign those s people to the same group.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; groupThePeople(vector&lt;int&gt;&amp; groupSizes) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    const int n =  groupSizes.size();
    unordered_map&lt;int, vector&lt;int&gt;&gt; m;
    for (int i = 0; i &lt; n; ++i) {
      auto&amp; v = m[groupSizes[i]];
      v.push_back(i);
      if (v.size() == groupSizes[i])
        ans.push_back(std::move(v));                       
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1282-group-the-people-given-the-group-size-they-belong-to/">花花酱 LeetCode 1282. Group the People Given the Group Size They Belong To</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/hashtable/leetcode-1282-group-the-people-given-the-group-size-they-belong-to/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 846. Hand of Straights</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-846-hand-of-straights/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-846-hand-of-straights/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Jun 2018 17:43:13 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2898</guid>

					<description><![CDATA[<p>Problem 题目大意：给你一些牌，问你能否分组，要求每组w张连续的牌。 https://leetcode.com/problems/hand-of-straights/description/ Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-846-hand-of-straights/">花花酱 LeetCode 846. Hand of Straights</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>题目大意：给你一些牌，问你能否分组，要求每组w张连续的牌。</p>
<p><a href="https://leetcode.com/problems/hand-of-straights/description/">https://leetcode.com/problems/hand-of-straights/description/</a></p>
<p>Alice has a <code>hand</code> of cards, given as an array of integers.</p>
<p>Now she wants to rearrange the cards into groups so that each group is size <code>W</code>, and consists of <code>W</code> consecutive cards.</p>
<p>Return <code>true</code> if and only if she can.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>hand = [1,2,3,6,2,3,4,7,8], W = 3
<strong>Output: </strong>true
<strong>Explanation:</strong> Alice's <code>hand</code> can be rearranged as <code>[1,2,3],[2,3,4],[6,7,8]</code>.</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>hand = [1,2,3,4,5], W = 4
<strong>Output: </strong>false
<strong>Explanation:</strong> Alice's <code>hand</code> can't be rearranged into groups of <code>4</code>.</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= hand.length &lt;= 10000</code></li>
<li><code>0 &lt;= hand[i] &lt;= 10^9</code></li>
<li><code>1 &lt;= W &lt;= hand.length</code></li>
</ol>
<h1><strong>Solution: Greedy</strong></h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 65 ms
class Solution {
public:
  bool isNStraightHand(vector&lt;int&gt;&amp; hand, int W) {
    map&lt;int, int&gt; counts;
    for (int h : hand) ++counts[h];
    while (!counts.empty()) {
      auto it = counts.begin();
      int start = it-&gt;first;
      for (int j = 0; j &lt; W; ++j) {        
        if (it == counts.end() 
            || it-&gt;first != start + j) return false;
        auto prev = it++;
        if (--(prev-&gt;second) == 0) 
          counts.erase(prev);
      }
    }    
    return true;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-846-hand-of-straights/">花花酱 LeetCode 846. Hand of Straights</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/greedy/leetcode-846-hand-of-straights/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
