<?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>gready Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/gready/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/gready/</link>
	<description></description>
	<lastBuildDate>Mon, 31 Jan 2022 00:34:51 +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>gready Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/gready/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2141. Maximum Running Time of N Computers</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2141-maximum-running-time-of-n-computers/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2141-maximum-running-time-of-n-computers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 31 Jan 2022 00:28:09 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[gready]]></category>
		<category><![CDATA[hard]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9446</guid>

					<description><![CDATA[<p>You have&#160;n&#160;computers. You are given the integer&#160;n&#160;and a&#160;0-indexed&#160;integer array&#160;batteries&#160;where the&#160;ith&#160;battery can&#160;run&#160;a computer for&#160;batteries[i]&#160;minutes. You are interested in running&#160;all&#160;n&#160;computers&#160;simultaneously&#160;using the given batteries. Initially, you can insert&#160;at&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2141-maximum-running-time-of-n-computers/">花花酱 LeetCode 2141. Maximum Running Time of N Computers</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>You have&nbsp;<code>n</code>&nbsp;computers. You are given the integer&nbsp;<code>n</code>&nbsp;and a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>batteries</code>&nbsp;where the&nbsp;<code>i<sup>th</sup></code>&nbsp;battery can&nbsp;<strong>run</strong>&nbsp;a computer for&nbsp;<code>batteries[i]</code>&nbsp;minutes. You are interested in running&nbsp;<strong>all</strong>&nbsp;<code>n</code>&nbsp;computers&nbsp;<strong>simultaneously</strong>&nbsp;using the given batteries.</p>



<p>Initially, you can insert&nbsp;<strong>at most one battery</strong>&nbsp;into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery&nbsp;<strong>any number of times</strong>. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.</p>



<p>Note that the batteries cannot be recharged.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>maximum</strong>&nbsp;number of minutes you can run all the&nbsp;</em><code>n</code><em>&nbsp;computers simultaneously.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/01/06/example1-fit.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, batteries = [3,3,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
Initially, insert battery 0 into the first computer and battery 1 into the second computer.
After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute.
At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead.
By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running.
We can run the two computers simultaneously for at most 4 minutes, so we return 4.

</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/01/06/example2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, batteries = [1,1,1,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
Initially, insert battery 0 into the first computer and battery 2 into the second computer. 
After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer. 
After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.
We can run the two computers simultaneously for at most 2 minutes, so we return 2.
</pre>



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



<ul><li><code>1 &lt;= n &lt;= batteries.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= batteries[i] &lt;= 10<sup>9</sup></code></li></ul>



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



<p>Find the smallest L that we can not run, ans = L &#8211; 1.</p>



<p>For a guessing m, we check the total battery powers T = sum(min(m, batteries[i])), if T >= m * n, it means there is a way (doesn&#8217;t need to figure out how) to run n computers for m minutes by fully unitize those batteries.</p>



<p>Proof: If T >= m*n holds, there are two cases:</p>



<ol><li>There are only n batteries, can not swap, but each of them has power >= m.</li><li>At least one of the batteries have power less than m, but there are more than n batteries and total power is sufficient, we can swap them with others.</li></ol>



<p>Time complexity: O(Slogn) where S = sum(batteries)<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:
  long long maxRunTime(int n, vector&lt;int&gt;&amp; batteries) {
    long long l = 0;
    long long r = accumulate(begin(batteries), end(batteries), 0LL) + 1;
    while (l &lt; r) {
      long long m = l + (r - l) / 2;
      long long t = 0;
      for (long long b : batteries)
        t += min(m, b);
      if (m * n &gt; t) // smallest m that does not fit.
        r = m;
      else
        l = m + 1;
    }
    return l - 1; // greatest m that fits.
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2141-maximum-running-time-of-n-computers/">花花酱 LeetCode 2141. Maximum Running Time of N Computers</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-2141-maximum-running-time-of-n-computers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1909. Remove One Element to Make the Array Strictly Increasing</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1909-remove-one-element-to-make-the-array-strictly-increasing/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1909-remove-one-element-to-make-the-array-strictly-increasing/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 24 Dec 2021 08:31:24 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[gready]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9219</guid>

					<description><![CDATA[<p>Given a&#160;0-indexed&#160;integer array&#160;nums, return&#160;true&#160;if it can be made&#160;strictly increasing&#160;after removing&#160;exactly one&#160;element, or&#160;false&#160;otherwise. If the array is already strictly increasing, return&#160;true. The array&#160;nums&#160;is&#160;strictly increasing&#160;if&#160;nums[i - 1]&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1909-remove-one-element-to-make-the-array-strictly-increasing/">花花酱 LeetCode 1909. Remove One Element to Make the Array Strictly Increasing</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;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>, return&nbsp;<code>true</code>&nbsp;<em>if it can be made&nbsp;<strong>strictly increasing</strong>&nbsp;after removing&nbsp;<strong>exactly one</strong>&nbsp;element, or&nbsp;</em><code>false</code><em>&nbsp;otherwise. If the array is already strictly increasing, return&nbsp;</em><code>true</code>.</p>



<p>The array&nbsp;<code>nums</code>&nbsp;is&nbsp;<strong>strictly increasing</strong>&nbsp;if&nbsp;<code>nums[i - 1] &lt; nums[i]</code>&nbsp;for each index&nbsp;<code>(1 &lt;= i &lt; nums.length).</code></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,10,5,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> By removing 10 at index 2 from nums, it becomes [1,2,5,7].
[1,2,5,7] is strictly increasing, so return true.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong>
[3,1,2] is the result of removing the element at index 0.
[2,1,2] is the result of removing the element at index 1.
[2,3,2] is the result of removing the element at index 2.
[2,3,1] is the result of removing the element at index 3.
No resulting array is strictly increasing, so return false.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> false
<strong>Explanation:</strong> The result of removing any element is [1,1].
[1,1] is not strictly increasing, so return false.
</pre>



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



<ul><li><code>2 &lt;= nums.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i] &lt;= 1000</code></li></ul>



<h2><strong>Solution 1: Brute Force</strong></h2>



<p>Enumerate the element to remove and check.</p>



<p>Time complexity: O(n<sup>2</sup>)<br>Space complexity: O(n) -> 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:
  bool canBeIncreasing(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    auto check = [&amp;](int k) {
      vector&lt;int&gt; arr(nums);
      arr.erase(begin(arr) + k);
      for (int i = 1; i &lt; n - 1; ++i)
        if (arr[i] &lt;= arr[i - 1]) return false;
      return true;
    };
    for (int i = 0; i &lt; n; ++i)
      if (check(i)) return true;
    return false;
  }
};</pre>

</div><h2 class="tabtitle">C++/O(1) Space</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool canBeIncreasing(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    auto check = [&amp;](int k) {      
      for (int i = 1; i &lt; n; ++i) {        
        int j = (i - 1 == k) ? (i - 2) : (i - 1);
        if (i != k &amp;&amp; j &gt;= 0 &amp;&amp; nums[i] &lt;= nums[j])          
          return false;
      }
      return true;
    };
    for (int i = 0; i &lt; n; ++i)
      if (check(i)) return true;
    return false;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1909-remove-one-element-to-make-the-array-strictly-increasing/">花花酱 LeetCode 1909. Remove One Element to Make the Array Strictly Increasing</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/array/leetcode-1909-remove-one-element-to-make-the-array-strictly-increasing/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
