<?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>preprocessing Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/preprocessing/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/preprocessing/</link>
	<description></description>
	<lastBuildDate>Mon, 28 Feb 2022 13:39:16 +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>preprocessing Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/preprocessing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2188. Minimum Time to Finish the Race</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2188-minimum-time-to-finish-the-race/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2188-minimum-time-to-finish-the-race/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 28 Feb 2022 12:56:59 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[preprocessing]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9516</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;2D integer array&#160;tires&#160;where&#160;tires[i] = [fi, ri]&#160;indicates that the&#160;ith&#160;tire can finish its&#160;xth&#160;successive lap in&#160;fi&#160;* ri(x-1)&#160;seconds. For example, if&#160;fi&#160;= 3&#160;and&#160;ri&#160;= 2, then the tire&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2188-minimum-time-to-finish-the-race/">花花酱 LeetCode 2188. Minimum Time to Finish the Race</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>0-indexed</strong>&nbsp;2D integer array&nbsp;<code>tires</code>&nbsp;where&nbsp;<code>tires[i] = [f<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;indicates that the&nbsp;<code>i<sup>th</sup></code>&nbsp;tire can finish its&nbsp;<code>x<sup>th</sup></code>&nbsp;successive lap in&nbsp;<code>f<sub>i</sub>&nbsp;* r<sub>i</sub><sup>(x-1)</sup></code>&nbsp;seconds.</p>



<ul><li>For example, if&nbsp;<code>f<sub>i</sub>&nbsp;= 3</code>&nbsp;and&nbsp;<code>r<sub>i</sub>&nbsp;= 2</code>, then the tire would finish its&nbsp;<code>1<sup>st</sup></code>&nbsp;lap in&nbsp;<code>3</code>&nbsp;seconds, its&nbsp;<code>2<sup>nd</sup></code>&nbsp;lap in&nbsp;<code>3 * 2 = 6</code>&nbsp;seconds, its&nbsp;<code>3<sup>rd</sup></code>&nbsp;lap in&nbsp;<code>3 * 2<sup>2</sup>&nbsp;= 12</code>&nbsp;seconds, etc.</li></ul>



<p>You are also given an integer&nbsp;<code>changeTime</code>&nbsp;and an integer&nbsp;<code>numLaps</code>.</p>



<p>The race consists of&nbsp;<code>numLaps</code>&nbsp;laps and you may start the race with&nbsp;<strong>any</strong>&nbsp;tire. You have an&nbsp;<strong>unlimited</strong>&nbsp;supply of each tire and after every lap, you may&nbsp;<strong>change</strong>&nbsp;to any given tire (including the current tire type) if you wait&nbsp;<code>changeTime</code>&nbsp;seconds.</p>



<p>Return<em>&nbsp;the&nbsp;<strong>minimum</strong>&nbsp;time to finish the race.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4
<strong>Output:</strong> 21
<strong>Explanation:</strong> 
Lap 1: Start with tire 0 and finish the lap in 2 seconds.
Lap 2: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds.
The minimum time to complete the race is 21 seconds.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5
<strong>Output:</strong> 25
<strong>Explanation:</strong> 
Lap 1: Start with tire 1 and finish the lap in 2 seconds.
Lap 2: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.
Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.
The minimum time to complete the race is 25 seconds. 
</pre>



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



<ul><li><code>1 &lt;= tires.length &lt;= 10<sup>5</sup></code></li><li><code>tires[i].length == 2</code></li><li><code>1 &lt;= f<sub>i</sub>, changeTime &lt;= 10<sup>5</sup></code></li><li><code>2 &lt;= r<sub>i</sub>&nbsp;&lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= numLaps &lt;= 1000</code></li></ul>



<h2><strong>Solution: DP</strong></h2>



<p>Observation: since ri &gt;= 2, we must change tire within 20 laps, otherwise it will be slower.</p>



<p>pre-compute the time to finish k laps using each type of tire (k &lt; 20), find min for each lap.</p>



<p>dp[i] = best[i], i &lt; 20,<br>dp[i] = min{dp[i &#8211; j] + changeTime + best[j]}, i &gt; 20</p>



<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(20)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumFinishTime(vector&lt;vector&lt;int&gt;&gt;&amp; tires, int changeTime, int numLaps) {
    constexpr int kMax = 20;
    vector&lt;long&gt; best(kMax, INT_MAX);
    for (const auto&amp; t : tires)
      for (long i = 1, l = t[0], s = t[0]; i &lt; kMax &amp;&amp; l &lt; t[0] + changeTime; ++i, l *= t[1], s += l)
        best[i] = min(best[i], s);
    vector&lt;long&gt; dp(numLaps + 1, INT_MAX);    
    for (int i = 1; i &lt;= numLaps; ++i)
      for (int j = 1; j &lt;= min(i, kMax - 1); ++j)
        dp[i] = min(dp[i], best[j] + (i &gt; j) * (changeTime + dp[i - j]));
    return dp[numLaps];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2188-minimum-time-to-finish-the-race/">花花酱 LeetCode 2188. Minimum Time to Finish the Race</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/dynamic-programming/leetcode-2188-minimum-time-to-finish-the-race/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
