<?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>k Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/k/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/k/</link>
	<description></description>
	<lastBuildDate>Sat, 21 Mar 2020 08:14:47 +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>k Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/k/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1383. Maximum Performance of a Team</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1383-maximum-performance-of-a-team/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1383-maximum-performance-of-a-team/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 21 Mar 2020 08:05:42 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[k]]></category>
		<category><![CDATA[sliding window]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6512</guid>

					<description><![CDATA[<p>There are&#160;n&#160;engineers numbered from 1 to&#160;n&#160;and&#160;two arrays:&#160;speed&#160;and&#160;efficiency, where&#160;speed[i]&#160;and&#160;efficiency[i]&#160;represent the speed and efficiency for the i-th engineer respectively.&#160;Return the maximum&#160;performance&#160;of a team composed of&#160;at most&#160;k&#160;engineers, since&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1383-maximum-performance-of-a-team/">花花酱 LeetCode 1383. Maximum Performance of a Team</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;engineers numbered from 1 to&nbsp;<code>n</code>&nbsp;and&nbsp;two arrays:&nbsp;<code>speed</code>&nbsp;and&nbsp;<code>efficiency</code>, where&nbsp;<code>speed[i]</code>&nbsp;and&nbsp;<code>efficiency[i]</code>&nbsp;represent the speed and efficiency for the i-th engineer respectively.&nbsp;<em>Return the maximum&nbsp;<strong>performance</strong>&nbsp;of a team composed of&nbsp;at most&nbsp;<code>k</code>&nbsp;engineers, since the answer can be a huge number, return this modulo&nbsp;10^9 + 7.</em></p>



<p>The&nbsp;<strong>performance</strong>&nbsp;of a team is the sum of their engineers&#8217; speeds multiplied by the minimum efficiency among&nbsp;their engineers.&nbsp;</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
<strong>Output:</strong> 60
<strong>Explanation:</strong> 
We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
<strong>Output:</strong> 68
<strong>Explanation:
</strong>This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4
<strong>Output:</strong> 72
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 10^5</code></li><li><code>speed.length == n</code></li><li><code>efficiency.length == n</code></li><li><code>1 &lt;= speed[i] &lt;= 10^5</code></li><li><code>1 &lt;= efficiency[i] &lt;= 10^8</code></li><li><code>1 &lt;= k &lt;= n</code></li></ul>



<h2><strong>Solution: Greedy</strong> + <strong>Sliding Window</strong></h2>



<ol><li>Sort engineers by their efficiency in descending order.</li><li>For each window of K engineers (we can have less than K people in the first k-1 windows), ans is sum(speed) * min(efficiency).</li></ol>



<p>Time complexity: O(nlogn) + O(nlogk)<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:
  int maxPerformance(int n, vector&lt;int&gt;&amp; speed, vector&lt;int&gt;&amp; efficiency, int k) {
    vector&lt;pair&lt;int, int&gt;&gt; es;
    for (int i = 0; i &lt; n; ++i)
      es.push_back({efficiency[i], speed[i]});
    sort(rbegin(es), rend(es));
    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; q;
    long sum = 0;
    long ans = 0;
    for (int i = 0; i &lt; n; ++i) {
      if (i &gt;= k) {
        sum -= q.top();
        q.pop();
      }
      sum += es[i].second;
      q.push(es[i].second);
      ans = max(ans, sum * es[i].first);
    }
    return ans % static_cast&lt;int&gt;(1e9 + 7);
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -&gt; int:
    speeds = 0
    ans = 0
    q = []
    for e, s in sorted(zip(efficiency, speed), reverse=True):
      if len(q) &gt;= k:
        speeds -= heapq.heappop(q)
      speeds += s
      heapq.heappush(q, s)
      ans = max(ans, speeds * e)
    return ans % (10**9 + 7)</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1383-maximum-performance-of-a-team/">花花酱 LeetCode 1383. Maximum Performance of a Team</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-1383-maximum-performance-of-a-team/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 14 Feb 2020 06:28:10 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[k]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sliding window]]></category>
		<category><![CDATA[subarray]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6306</guid>

					<description><![CDATA[<p>Given an array of integers&#160;arr&#160;and two integers&#160;k&#160;and&#160;threshold. Return&#160;the number of sub-arrays&#160;of size&#160;k&#160;and average greater than or equal to&#160;threshold. Example 1: Input: arr = [2,2,2,2,5,5,5,8], k&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/">花花酱 LeetCode 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold</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 an array of integers&nbsp;<code>arr</code>&nbsp;and two integers&nbsp;<code>k</code>&nbsp;and&nbsp;<code>threshold</code>.</p>



<p>Return&nbsp;<em>the number of sub-arrays</em>&nbsp;of size&nbsp;<code>k</code>&nbsp;and average greater than or equal to&nbsp;<code>threshold</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,1,1,1,1], k = 1, threshold = 0
<strong>Output:</strong> 5
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
<strong>Output:</strong> 6
<strong>Explanation:</strong> The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [7,7,7,7,7,7,7], k = 7, threshold = 7
<strong>Output:</strong> 1
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [4,4,4,4], k = 4, threshold = 1
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>1 &lt;= arr.length &lt;= 10^5</code></li><li><code>1 &lt;= arr[i] &lt;= 10^4</code></li><li><code>1 &lt;= k &lt;= arr.length</code></li><li><code>0 &lt;= threshold &lt;= 10^4</code></li></ul>



<h2><strong>Solution: Sliding Window</strong></h2>



<ol><li>Window size = k</li><li>Maintain the sum of the window</li><li>Check sum &gt;= threshold * k</li></ol>



<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">// Author: Huahua
class Solution {
public:
  int numOfSubarrays(vector&lt;int&gt;&amp; arr, int k, int threshold) {
    int ans = 0;
    int sum = 0;
    for (int i = 0; i &lt; arr.size(); ++i) {
      sum += arr[i];
      if (i + 1 &gt;= k) {
        if (threshold * k &lt;= sum) ++ans;
        sum -= arr[i + 1 - k];
      } 
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/">花花酱 LeetCode 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold</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/sliding-window/leetcode-1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 523. Continuous Subarray Sum</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-523-continuous-subarray-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-523-continuous-subarray-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Jul 2018 14:59:05 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[k]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix sum]]></category>
		<category><![CDATA[reminder]]></category>
		<category><![CDATA[subarray]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3127</guid>

					<description><![CDATA[<p>Problem Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-523-continuous-subarray-sum/">花花酱 LeetCode 523. Continuous Subarray 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[<h1><strong>Problem</strong></h1>
<p>Given a list of <b>non-negative</b> numbers and a target <b>integer</b> k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of <b>k</b>, that is, sums up to n*k where n is also an <b>integer</b>.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> [23, 2, 4, 6, 7],  k=6
<b>Output:</b> True
<b>Explanation:</b> Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b> [23, 2, 6, 4, 7],  k=6
<b>Output:</b> True
<b>Explanation:</b> Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
</pre>
<p><b>Note:</b></p>
<ol>
<li>The length of the array won&#8217;t exceed 10,000.</li>
<li>You may assume the sum of all the numbers is in the range of a signed 32-bit integer.</li>
</ol>
<h1><strong>Special case: </strong></h1>
<p>nums = [0,0], k = 0, return = True</p>
<h1><strong>Solution: Prefix Sum Reminder</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(min(n, k))</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 16 ms (&lt;99.62%)
class Solution {
public:
  bool checkSubarraySum(vector&lt;int&gt;&amp; nums, int k) {    
    unordered_map&lt;int, int&gt; m; // sum % k -&gt; first index
    m[0] = -1;
    int sum = 0;
    for (int i = 0; i &lt; nums.size(); ++i) {
      sum += nums[i];
      if (k != 0) sum %= k;      
      if (m.count(sum)) {
        if (i - m.at(sum) &gt; 1) return true;
      } else {
        m[sum] = i;
      }
    }
    return false;
  }
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/hashtable/leetcode-560-subarray-sum-equals-k/">花花酱 LeetCode 560. Subarray Sum Equals K</a></li>
<li><a href="http://zxi.mytechroad.com/blog/hashtable/leetcode-525-contiguous-array/">花花酱 LeetCode 525. Contiguous Array</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-523-continuous-subarray-sum/">花花酱 LeetCode 523. Continuous Subarray 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/hashtable/leetcode-523-continuous-subarray-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
