{"id":8775,"date":"2021-11-26T08:29:15","date_gmt":"2021-11-26T16:29:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8775"},"modified":"2021-11-26T08:33:06","modified_gmt":"2021-11-26T16:33:06","slug":"leetcode-2008-maximum-earnings-from-taxi","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2008-maximum-earnings-from-taxi\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2008. Maximum Earnings From Taxi"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;points on a road you are driving your taxi on. The&nbsp;<code>n<\/code>&nbsp;points on the road are labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>&nbsp;in the direction you are going, and you want to drive from point&nbsp;<code>1<\/code>&nbsp;to point&nbsp;<code>n<\/code>&nbsp;to make money by picking up passengers. You cannot change the direction of the taxi.<\/p>\n\n\n\n<p>The passengers are represented by a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>rides<\/code>, where&nbsp;<code>rides[i] = [start<sub>i<\/sub>, end<sub>i<\/sub>, tip<sub>i<\/sub>]<\/code>&nbsp;denotes the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;passenger requesting a ride from point&nbsp;<code>start<sub>i<\/sub><\/code>&nbsp;to point&nbsp;<code>end<sub>i<\/sub><\/code>&nbsp;who is willing to give a&nbsp;<code>tip<sub>i<\/sub><\/code>&nbsp;dollar tip.<\/p>\n\n\n\n<p>For<strong>&nbsp;each&nbsp;<\/strong>passenger&nbsp;<code>i<\/code>&nbsp;you pick up, you&nbsp;<strong>earn<\/strong>&nbsp;<code>end<sub>i<\/sub>&nbsp;- start<sub>i<\/sub>&nbsp;+ tip<sub>i<\/sub><\/code>&nbsp;dollars. You may only drive&nbsp;<strong>at most one&nbsp;<\/strong>passenger at a time.<\/p>\n\n\n\n<p>Given&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>rides<\/code>, return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of dollars you can earn by picking up the passengers optimally.<\/em><\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;You may drop off a passenger and pick up a different passenger at the same point.<\/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> n = 5, rides = [[2,5,4],[1,5,1]]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.\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> n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]]\n<strong>Output:<\/strong> 20\n<strong>Explanation:<\/strong> We will pick up the following passengers:\n- Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.\n- Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.\n- Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.\nWe earn 9 + 5 + 6 = 20 dollars in total.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= rides.length &lt;= 3 * 10<sup>4<\/sup><\/code><\/li><li><code>rides[i].length == 3<\/code><\/li><li><code>1 &lt;= start<sub>i<\/sub>&nbsp;&lt; end<sub>i<\/sub>&nbsp;&lt;= n<\/code><\/li><li><code>1 &lt;= tip<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i] := max earnings we can get at position i and the taxi is empty.<\/p>\n\n\n\n<p>dp[i] = max(dp[i &#8211; 1], dp[s] + gain) where e = i, gain = e &#8211; s + tips<\/p>\n\n\n\n<p>For each i, we check all the rides that end at i and find the best one (which may have different starting points), otherwise the earning will be the same as previous position (i &#8211; 1).<\/p>\n\n\n\n<p>answer = dp[n] <\/p>\n\n\n\n<p>Time complexity: O(m + n)<br>Space complexity: O(m + n)<\/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  long long maxTaxiEarnings(int n, vector<vector<int>>& rides) {    \n    vector<long long> dp(n + 1);\n    vector<vector<pair<int, int>>> m(n + 1);\n    for (const auto& r : rides)\n      m[r[1]].emplace_back(r[0], r[1] - r[0] + r[2]);\n    for (int i = 1; i <= n; ++i) {\n      dp[i] = dp[i - 1];\n      for (const auto [s, g] : m[i])\n        dp[i] = max(dp[i], dp[s] + g);\n    }\n    return dp[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;points on a road you are driving your taxi on. The&nbsp;n&nbsp;points on the road are labeled from&nbsp;1&nbsp;to&nbsp;n&nbsp;in the direction you are going, and you&#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,177],"class_list":["post-8775","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8775","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=8775"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8775\/revisions"}],"predecessor-version":[{"id":8778,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8775\/revisions\/8778"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}