<?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>ceil Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ceil/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ceil/</link>
	<description></description>
	<lastBuildDate>Fri, 06 Aug 2021 04:37:49 +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>ceil Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ceil/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1870. Minimum Speed to Arrive on Time</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1870-minimum-speed-to-arrive-on-time/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1870-minimum-speed-to-arrive-on-time/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 06 Aug 2021 04:35:21 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[ceil]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8484</guid>

					<description><![CDATA[<p>You are given a floating-point number&#160;hour, representing the amount of time you have to reach the office. To commute to the office, you must take&#160;n&#160;trains&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1870-minimum-speed-to-arrive-on-time/">花花酱 LeetCode 1870. Minimum Speed to Arrive on Time</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 floating-point number&nbsp;<code>hour</code>, representing the amount of time you have to reach the office. To commute to the office, you must take&nbsp;<code>n</code>&nbsp;trains in sequential order. You are also given an integer array&nbsp;<code>dist</code>&nbsp;of length&nbsp;<code>n</code>, where&nbsp;<code>dist[i]</code>&nbsp;describes the distance (in kilometers) of the&nbsp;<code>i<sup>th</sup></code>&nbsp;train ride.</p>



<p>Each train can only depart at an integer hour, so you may need to wait in between each train ride.</p>



<ul><li>For example, if the&nbsp;<code>1<sup>st</sup></code>&nbsp;train ride takes&nbsp;<code>1.5</code>&nbsp;hours, you must wait for an additional&nbsp;<code>0.5</code>&nbsp;hours before you can depart on the&nbsp;<code>2<sup>nd</sup></code>&nbsp;train ride at the 2 hour mark.</li></ul>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum positive integer</strong>&nbsp;speed&nbsp;<strong>(in kilometers per hour)</strong>&nbsp;that all the trains must travel at for you to reach the office on time, or&nbsp;</em><code>-1</code><em>&nbsp;if it is impossible to be on time</em>.</p>



<p>Tests are generated such that the answer will not exceed&nbsp;<code>10<sup>7</sup></code>&nbsp;and&nbsp;<code>hour</code>&nbsp;will have&nbsp;<strong>at most two digits after the decimal point</strong>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> dist = [1,3,2], hour = 6
<strong>Output:</strong> 1
<strong>Explanation: </strong>At speed 1:
- The first train ride takes 1/1 = 1 hour.
- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours.
- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours.
- You will arrive at exactly the 6 hour mark.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> dist = [1,3,2], hour = 2.7
<strong>Output:</strong> 3
<strong>Explanation: </strong>At speed 3:
- The first train ride takes 1/3 = 0.33333 hours.
- Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour.
- Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours.
- You will arrive at the 2.66667 hour mark.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> dist = [1,3,2], hour = 1.9
<strong>Output:</strong> -1
<strong>Explanation:</strong> It is impossible because the earliest the third train can depart is at the 2 hour mark.
</pre>



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



<ul><li><code>n == dist.length</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= dist[i] &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= hour &lt;= 10<sup>9</sup></code></li><li>There will be at most two digits after the decimal point in&nbsp;<code>hour</code>.</li></ul>



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



<p>l = speed<sub>min</sub>=1<br>r = speed<sub>max</sub>+1 = 1e7 + 1</p>



<p>Find the min valid speed m such that t(m) &lt;= hour.</p>



<p>Time complexity: O(nlogn)<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 minSpeedOnTime(vector&lt;int&gt;&amp; dist, double hour) {
    constexpr int kMax = 1e7 + 1;
    const int n = dist.size();    
    int l = 1;
    int r = kMax;    
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      int t = 0;
      for (int i = 0; i &lt; n - 1; ++i)
        t += (dist[i] + m - 1) / m;
      if (t + dist.back() * 1.0 / m &lt;= hour)
        r = m;
      else
        l = m + 1;      
    }
    return l == kMax ? -1 : l;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1870-minimum-speed-to-arrive-on-time/">花花酱 LeetCode 1870. Minimum Speed to Arrive on Time</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-1870-minimum-speed-to-arrive-on-time/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
