{"id":9168,"date":"2021-12-12T17:52:00","date_gmt":"2021-12-13T01:52:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9168"},"modified":"2021-12-12T17:52:38","modified_gmt":"2021-12-13T01:52:38","slug":"leetcode-2105-watering-plants-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-2105-watering-plants-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2105. Watering Plants II"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong><a href=\"https:\/\/leetcode.com\/problems\/watering-plants-ii\/\">Problem<\/a><\/strong><\/h2>\n\n\n\n<p>Solution: Simulation w\/ Two Pointers<\/p>\n\n\n\n<p>Simulate the watering process.<\/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 minimumRefill(vector<int>& plants, int capacityA, int capacityB) {\n    const int n = plants.size();    \n    int ans = 0;\n    for (int l = 0, r = n - 1, A = capacityA, B = capacityB; l <= r; ++l, --r) {      \n      if (l == r)\n        return ans += max(A, B) < plants[l];\n      if ((A -= plants[l]) < 0) \n        A = capacityA - plants[l], ++ans;        \n      if ((B -= plants[r]) < 0) \n        B = capacityB - plants[r], ++ans;     \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Solution: Simulation w\/ Two Pointers Simulate the watering process. Time complexity: O(n)Space complexity: O(1) \/\/ Author: Huahua class Solution { public: int minimumRefill(vector&#038; plants,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[20,177,179,175],"class_list":["post-9168","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-array","tag-medium","tag-simulation","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9168","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=9168"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9168\/revisions"}],"predecessor-version":[{"id":9170,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9168\/revisions\/9170"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}