{"id":9171,"date":"2021-12-12T18:49:45","date_gmt":"2021-12-13T02:49:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9171"},"modified":"2021-12-14T22:01:13","modified_gmt":"2021-12-15T06:01:13","slug":"leetcode-2106-maximum-fruits-harvested-after-at-most-k-steps","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2106-maximum-fruits-harvested-after-at-most-k-steps\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2106. Maximum Fruits Harvested After at Most K Steps"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong><a href=\"http:\/\/2106. Maximum Fruits Harvested After at Most K Steps\">Problem<\/a><\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Range sum query<\/strong><\/h2>\n\n\n\n<p>Assuming we can collect fruits in range [l, r], we need a fast query to compute the sum of those fruits.<\/p>\n\n\n\n<p>Given startPos and k, we have four options:<br>1. move i steps to the left<br>2. move i steps to the left and k &#8211; i steps to the right.<br>3. move i steps to the right<br>4. move i steps to the right and k &#8211; i steps to the left.<\/p>\n\n\n\n<p>We enumerate i steps and calculate maximum range [l, r] covered by each option, and collect all the fruit in that range.<\/p>\n\n\n\n<p>Time complexity: O(m + k)<br>Space complexity: O(m)<br>where m = max(max(pos), startPos)<\/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 maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {    \n    const int m = max(startPos, (*max_element(begin(fruits), end(fruits)))[0]);\n    vector<int> sums(m + 2);   \n    for (int i = 0, j = 0; i <= m; ++i) {\n      sums[i + 1] += sums[i];\n      while (j < fruits.size() &#038;&#038; fruits[j][0] == i)        \n        sums[i + 1] += fruits[j++][1];      \n    }    \n    int ans = 0;\n    for (int s = 0; s <= k; ++s) {\n      if (startPos - s >= 0) {\n        int l = startPos - s;\n        int r = min(max(startPos, l + (k - s)), m);        \n        ans = max(ans, sums[r + 1] - sums[l]);\n      }\n      if (startPos + s <= m) {\n        int r = startPos + s;\n        int l = max(0, min(startPos, r - (k - s)));\n        ans = max(ans, sums[r + 1] - sums[l]);\n      }\n    }             \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Maintain a window [l, r] such that the steps to cover [l, r] from startPos is less or equal to k.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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 maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {    \n    auto steps = [&](int l, int r) {\n      if (r <= startPos)\n        return startPos - l;\n      else if (l >= startPos)\n        return r - startPos;\n      else\n        return min(startPos + r - 2 * l, 2 * r - startPos - l);\n    };\n    int ans = 0;\n    for (int r = 0, l = 0, cur = 0; r < fruits.size(); ++r) {\n      cur += fruits[r][1];\n      while (l <= r &#038;&#038; steps(fruits[l][0], fruits[r][0]) > k)\n        cur -= fruits[l++][1];      \n      ans = max(ans, cur);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Solution 1: Range sum query Assuming we can collect fruits in range [l, r], we need a fast query to compute the sum of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[217,140,139,98,215,62],"class_list":["post-9171","post","type-post","status-publish","format-standard","hentry","category-array","tag-hard","tag-query","tag-range","tag-range-query","tag-sliding-window","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9171","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=9171"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9171\/revisions"}],"predecessor-version":[{"id":9178,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9171\/revisions\/9178"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}