{"id":9516,"date":"2022-02-28T04:56:59","date_gmt":"2022-02-28T12:56:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9516"},"modified":"2022-02-28T05:39:16","modified_gmt":"2022-02-28T13:39:16","slug":"leetcode-2188-minimum-time-to-finish-the-race","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2188-minimum-time-to-finish-the-race\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2188. Minimum Time to Finish the Race"},"content":{"rendered":"\n<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>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<p>You are also given an integer&nbsp;<code>changeTime<\/code>&nbsp;and an integer&nbsp;<code>numLaps<\/code>.<\/p>\n\n\n\n<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>\n\n\n\n<p>Return<em>&nbsp;the&nbsp;<strong>minimum<\/strong>&nbsp;time to finish the race.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4\n<strong>Output:<\/strong> 21\n<strong>Explanation:<\/strong> \nLap 1: Start with tire 0 and finish the lap in 2 seconds.\nLap 2: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.\nLap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.\nLap 4: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.\nTotal time = 2 + 6 + 5 + 2 + 6 = 21 seconds.\nThe minimum time to complete the race is 21 seconds.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5\n<strong>Output:<\/strong> 25\n<strong>Explanation:<\/strong> \nLap 1: Start with tire 1 and finish the lap in 2 seconds.\nLap 2: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.\nLap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.\nLap 4: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.\nLap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.\nTotal time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.\nThe minimum time to complete the race is 25 seconds. \n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Observation: since ri &gt;= 2, we must change tire within 20 laps, otherwise it will be slower.<\/p>\n\n\n\n<p>pre-compute the time to finish k laps using each type of tire (k &lt; 20), find min for each lap.<\/p>\n\n\n\n<p>dp[i] = best[i], i &lt; 20,<br>dp[i] = min{dp[i &#8211; j] + changeTime + best[j]}, i &gt; 20<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(20)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minimumFinishTime(vector<vector<int>>& tires, int changeTime, int numLaps) {\n    constexpr int kMax = 20;\n    vector<long> best(kMax, INT_MAX);\n    for (const auto& t : tires)\n      for (long i = 1, l = t[0], s = t[0]; i < kMax &#038;&#038; l < t[0] + changeTime; ++i, l *= t[1], s += l)\n        best[i] = min(best[i], s);\n    vector<long> dp(numLaps + 1, INT_MAX);    \n    for (int i = 1; i <= numLaps; ++i)\n      for (int j = 1; j <= min(i, kMax - 1); ++j)\n        dp[i] = min(dp[i], best[j] + (i > j) * (changeTime + dp[i - j]));\n    return dp[numLaps];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;2D integer array&nbsp;tires&nbsp;where&nbsp;tires[i] = [fi, ri]&nbsp;indicates that the&nbsp;ith&nbsp;tire can finish its&nbsp;xth&nbsp;successive lap in&nbsp;fi&nbsp;* ri(x-1)&nbsp;seconds. For example, if&nbsp;fi&nbsp;= 3&nbsp;and&nbsp;ri&nbsp;= 2, then the tire&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,217,765],"class_list":["post-9516","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-preprocessing","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9516"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9516\/revisions"}],"predecessor-version":[{"id":9518,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9516\/revisions\/9518"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}