<?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>sweepline Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/sweepline/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/sweepline/</link>
	<description></description>
	<lastBuildDate>Mon, 29 Apr 2019 05:33:38 +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>sweepline Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/sweepline/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 853. Car Fleet</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-853-car-fleet/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-853-car-fleet/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 29 Apr 2019 05:19:52 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sweepline]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5124</guid>

					<description><![CDATA[<p>N&#160;cars are going to the same destination along a one lane road.&#160; The destination is&#160;target&#160;miles away. Each car&#160;i&#160;has a constant speed&#160;speed[i]&#160;(in miles per hour), and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-853-car-fleet/">花花酱 LeetCode 853. Car Fleet</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><code>N</code>&nbsp;cars are going to the same destination along a one lane road.&nbsp; The destination is&nbsp;<code>target</code>&nbsp;miles away.</p>



<p>Each car&nbsp;<code>i</code>&nbsp;has a constant speed&nbsp;<code>speed[i]</code>&nbsp;(in miles per hour), and initial position&nbsp;<code>position[i]</code>&nbsp;miles towards the target along the road.</p>



<p>A car can never pass another car ahead of it, but it can catch up to it, and drive bumper to bumper at the same speed.</p>



<p>The distance between these two cars is ignored &#8211; they are assumed to have the same position.</p>



<p>A&nbsp;<em>car fleet</em>&nbsp;is some non-empty set of cars driving&nbsp;at the same position and same speed.&nbsp; Note that a single car is also a car fleet.</p>



<p>If a car catches up to a car fleet right at the destination point, it will&nbsp;still be&nbsp;considered as one car fleet.</p>



<p><br>How many car fleets will arrive at the destination?</p>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input: </strong>target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
<strong>Output: </strong>3
<strong>Explanation</strong>:
The cars starting at 10 and 8 become a fleet, meeting each other at 12.
The car starting at 0 doesn't catch up to any other car, so it is a fleet by itself.
The cars starting at 5 and 3 become a fleet, meeting each other at 6.
Note that no other cars meet these fleets before the destination, so the answer is 3.
</pre>



<p><br><strong>Note:</strong></p>



<ol><li><code>0 &lt;= N &lt;= 10 ^ 4</code></li><li><code>0 &lt; target&nbsp;&lt;= 10 ^ 6</code></li><li><code>0 &lt;&nbsp;speed[i] &lt;= 10 ^ 6</code></li><li><code>0 &lt;= position[i] &lt; target</code></li><li>All initial positions are different.</li></ol>



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



<ol><li>Compute the time when each car can reach target.</li><li> Sort cars by position DESC </li></ol>



<p>Answer will be number slow cars in the time array.</p>



<pre class="wp-block-preformatted; crayon:false">
Ex 1: 
target = 12
p = [10,8,0,5,3] 
v = [2,4,1,1,3]


p     t
10    1  <- slow -- ^
 8    1             |
 5    7  <- slow -- ^
 3    3             |
 0   12  <- slow -- ^

Ex 2
target = 10
p = [5, 4, 3, 2, 1]
v = [1, 2, 3, 4, 5]

p     t
5     5  <- slow -- ^
4     3             |
3     2.33          |
2     2             |
1     1.8           |

</pre>



<p>Time complexity: O(nlogn)<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, running time: 52 ms / 10.9 MB
class Solution {
public:
  int carFleet(int target, vector&lt;int&gt;&amp; position, vector&lt;int&gt;&amp; speed) {
    vector&lt;pair&lt;int, float&gt;&gt; cars(position.size());
    for (int i = 0; i &lt; position.size(); ++i)
      cars[i] = {position[i], static_cast&lt;float&gt;(target - position[i]) / speed[i]};
    sort(rbegin(cars), rend(cars));    
    int ans{0};
    float max_t{0};
    for (const auto&amp; [p, t] : cars)       
      if (t &gt; max_t) {
        max_t = t;
        ++ans;
      }    
    return ans;
  }
};</pre>

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

<pre class="crayon-plain-tag">class Solution:
  def carFleet(self, target: int, position: List[int], speed: List[int]) -&gt; int:
    cars = sorted([(p, (target - p) / s) for p, s in zip(position, speed)], 
                  key=lambda x: -x[0])
    ans, max_t = 0, 0    
    for p, t in cars:
      if t &gt; max_t:
        ans += 1
        max_t = t
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-853-car-fleet/">花花酱 LeetCode 853. Car Fleet</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-853-car-fleet/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
