<?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>diff Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/diff/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/diff/</link>
	<description></description>
	<lastBuildDate>Fri, 31 Dec 2021 13:17:29 +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>diff Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/diff/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1936. Add Minimum Number of Rungs</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1936-add-minimum-number-of-rungs/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1936-add-minimum-number-of-rungs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 13:14:17 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[diff]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9302</guid>

					<description><![CDATA[<p>You are given a&#160;strictly increasing&#160;integer array&#160;rungs&#160;that represents the&#160;height&#160;of rungs on a ladder. You are currently on the&#160;floor&#160;at height&#160;0, and you want to reach the last&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1936-add-minimum-number-of-rungs/">花花酱 LeetCode 1936. Add Minimum Number of Rungs</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 are given a&nbsp;<strong>strictly increasing</strong>&nbsp;integer array&nbsp;<code>rungs</code>&nbsp;that represents the&nbsp;<strong>height</strong>&nbsp;of rungs on a ladder. You are currently on the&nbsp;<strong>floor</strong>&nbsp;at height&nbsp;<code>0</code>, and you want to reach the last rung.</p>



<p>You are also given an integer&nbsp;<code>dist</code>. You can only climb to the next highest rung if the distance between where you are currently at (the floor or on a rung) and the next rung is&nbsp;<strong>at most</strong>&nbsp;<code>dist</code>. You are able to insert rungs at any positive&nbsp;<strong>integer</strong>&nbsp;height if a rung is not already there.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of rungs that must be added to the ladder in order for you to climb to the last rung.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> rungs = [1,3,5,10], dist = 2
<strong>Output:</strong> 2
<strong>Explanation:
</strong>You currently cannot reach the last rung.
Add rungs at heights 7 and 8 to climb this ladder. 
The ladder will now have rungs at [1,3,5,7,8,10].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> rungs = [3,6,8,10], dist = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong>
This ladder can be climbed without adding additional rungs.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> rungs = [3,4,6,7], dist = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong>
You currently cannot reach the first rung from the ground.
Add a rung at height 1 to climb this ladder.
The ladder will now have rungs at [1,3,4,6,7].
</pre>



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



<ul><li><code>1 &lt;= rungs.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= rungs[i] &lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= dist &lt;= 10<sup>9</sup></code></li><li><code>rungs</code>&nbsp;is&nbsp;<strong>strictly increasing</strong>.</li></ul>



<h2><strong>Solution: Math</strong></h2>



<p>Check two consecutive rungs, if their diff is &gt; dist, we need insert (diff &#8211; 1) / dist rungs in between.<br>ex1 5 -&gt; 11, diff = 6, dist = 2, (diff &#8211; 1) / dist = (6 &#8211; 1) / 2 = 2. =&gt; 5, <span style="text-decoration: underline;">7, 9</span>, 11.<br>ex2 0 -&gt; 3, diff = 3, dist = 1, (diff &#8211; 1) / dist = (3 &#8211; 1) / 1 = 2 =&gt; 0, <span style="text-decoration: underline;">1, 2,</span> 3</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">// Author: Huahua
class Solution {
public:
  int addRungs(vector&lt;int&gt;&amp; rungs, int dist) {
    int ans = 0;
    for (size_t i = 0; i &lt; rungs.size(); ++i)
      ans += (rungs[i] - (i ? rungs[i - 1] : 0) - 1) / dist;    
    return ans;
  }
};</pre>

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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def addRungs(self, rungs: List[int], dist: int) -&gt; int:
    return sum((r - 1 - (rungs[i - 1] if i else 0)) // dist for i, r in enumerate(rungs))</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1936-add-minimum-number-of-rungs/">花花酱 LeetCode 1936. Add Minimum Number of Rungs</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/math/leetcode-1936-add-minimum-number-of-rungs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 May 2020 16:49:59 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[diff]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6857</guid>

					<description><![CDATA[<p>Given a rectangular cake with height&#160;h&#160;and width&#160;w, and two arrays of integers&#160;horizontalCuts&#160;and&#160;verticalCuts&#160;where&#160;horizontalCuts[i]&#160;is the distance from the top of the rectangular cake to the&#160;ith&#160;horizontal cut&#160;and similarly,&#160;verticalCuts[j]&#160;is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/">花花酱 LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts</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 rectangular cake with height&nbsp;<code>h</code>&nbsp;and width&nbsp;<code>w</code>, and two arrays of integers&nbsp;<code>horizontalCuts</code>&nbsp;and&nbsp;<code>verticalCuts</code>&nbsp;where&nbsp;<code>horizontalCuts[i]</code>&nbsp;is the distance from the top of the rectangular cake to the&nbsp;<code>ith</code>&nbsp;horizontal cut&nbsp;and similarly,&nbsp;<code>verticalCuts[j]</code>&nbsp;is the distance from the&nbsp;left of the rectangular cake to the&nbsp;<code>jth</code>&nbsp;vertical cut.</p>



<p><em>Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays&nbsp;<code>horizontalCuts</code>&nbsp;and&nbsp;<code>verticalCuts</code>.&nbsp;</em>Since the answer can be a huge number, return this modulo 10^9 + 7.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
<strong>Output:</strong> 4 
<strong>Explanation:</strong> The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
<strong>Output:</strong> 9
</pre>



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



<ul><li><code>2 &lt;= h,&nbsp;w &lt;= 10^9</code></li><li><code>1 &lt;=&nbsp;horizontalCuts.length &lt;&nbsp;min(h, 10^5)</code></li><li><code>1 &lt;=&nbsp;verticalCuts.length &lt; min(w, 10^5)</code></li><li><code>1 &lt;=&nbsp;horizontalCuts[i] &lt; h</code></li><li><code>1 &lt;=&nbsp;verticalCuts[i] &lt; w</code></li><li>It is guaranteed that all elements in&nbsp;<code>horizontalCuts</code>&nbsp;are distinct.</li><li>It is guaranteed that all elements in&nbsp;<code>verticalCuts</code>&nbsp;are distinct.</li></ul>



<h2><strong>Solution: Geometry</strong></h2>



<p>Find the max gap between vertical cuts mx and max gap between horizontal cuts my. ans = mx * my</p>



<p>Time complexity: O(nlogn)<br>Space complexity: O(1) if sort in place otherweise 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 maxArea(int h, int w, vector&lt;int&gt;&amp; horizontalCuts, vector&lt;int&gt;&amp; verticalCuts) {
    constexpr int kMod = 1e9 + 7;
    sort(begin(verticalCuts), end(verticalCuts));
    sort(begin(horizontalCuts), end(horizontalCuts));
    int mx = max(verticalCuts[0], w - verticalCuts.back());
    int my = max(horizontalCuts[0], h - horizontalCuts.back());
    for (int i = 1; i &lt; verticalCuts.size(); ++i)
      mx = max(mx, verticalCuts[i] - verticalCuts[i - 1]);
    for (int i = 1; i &lt; horizontalCuts.size(); ++i)
      my = max(my, horizontalCuts[i] - horizontalCuts[i - 1]);               
    return static_cast&lt;long&gt;(mx) * my % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/">花花酱 LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts</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/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
