{"id":8750,"date":"2021-11-20T23:59:10","date_gmt":"2021-11-21T07:59:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8750"},"modified":"2021-11-21T00:01:01","modified_gmt":"2021-11-21T08:01:01","slug":"leetcode-2079-watering-plants","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2079-watering-plants\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2079. Watering Plants"},"content":{"rendered":"\n<p>You want to water&nbsp;<code>n<\/code>&nbsp;plants in your garden with a watering can. The plants are arranged in a row and are labeled from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>&nbsp;from left to right where the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;plant is located at&nbsp;<code>x = i<\/code>. There is a river at&nbsp;<code>x = -1<\/code>&nbsp;that you can refill your watering can at.<\/p>\n\n\n\n<p>Each plant needs a specific amount of water. You will water the plants in the following way:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Water the plants in order from left to right.<\/li><li>After watering the current plant, if you do not have enough water to&nbsp;<strong>completely<\/strong>&nbsp;water the next plant, return to the river to fully refill the watering can.<\/li><li>You&nbsp;<strong>cannot<\/strong>&nbsp;refill the watering can early.<\/li><\/ul>\n\n\n\n<p>You are initially at the river (i.e.,&nbsp;<code>x = -1<\/code>). It takes&nbsp;<strong>one step<\/strong>&nbsp;to move&nbsp;<strong>one unit<\/strong>&nbsp;on the x-axis.<\/p>\n\n\n\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>plants<\/code>&nbsp;of&nbsp;<code>n<\/code>&nbsp;integers, where&nbsp;<code>plants[i]<\/code>&nbsp;is the amount of water the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;plant needs, and an integer&nbsp;<code>capacity<\/code>&nbsp;representing the watering can capacity, return&nbsp;<em>the&nbsp;<strong>number of steps<\/strong>&nbsp;needed to water all the plants<\/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> plants = [2,2,3,3], capacity = 5\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong> Start at the river with a full watering can:\n- Walk to plant 0 (1 step) and water it. Watering can has 3 units of water.\n- Walk to plant 1 (1 step) and water it. Watering can has 1 unit of water.\n- Since you cannot completely water plant 2, walk back to the river to refill (2 steps).\n- Walk to plant 2 (3 steps) and water it. Watering can has 2 units of water.\n- Since you cannot completely water plant 3, walk back to the river to refill (3 steps).\n- Walk to plant 3 (4 steps) and water it.\nSteps needed = 1 + 1 + 2 + 3 + 3 + 4 = 14.\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> plants = [1,1,1,4,2,3], capacity = 4\n<strong>Output:<\/strong> 30\n<strong>Explanation:<\/strong> Start at the river with a full watering can:\n- Water plants 0, 1, and 2 (3 steps). Return to river (3 steps).\n- Water plant 3 (4 steps). Return to river (4 steps).\n- Water plant 4 (5 steps). Return to river (5 steps).\n- Water plant 5 (6 steps).\nSteps needed = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> plants = [7,7,7,7,7,7,7], capacity = 8\n<strong>Output:<\/strong> 49\n<strong>Explanation:<\/strong> You have to refill before watering each plant.\nSteps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == plants.length<\/code><\/li><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>1 &lt;= plants[i] &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>max(plants[i]) &lt;= capacity &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\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 wateringPlants(vector<int>& plants, int capacity) {\n    const int n = plants.size();\n    int ans = 0;\n    for (int i = 0, c = capacity; i < n; c -= plants[i++], ++ans) {\n      if (c >= plants[i]) continue;\n      ans += i * 2;\n      c = capacity;      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You want to water&nbsp;n&nbsp;plants in your garden with a watering can. The plants are arranged in a row and are labeled from&nbsp;0&nbsp;to&nbsp;n &#8211; 1&nbsp;from left&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[20,177,179],"class_list":["post-8750","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8750","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=8750"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8750\/revisions"}],"predecessor-version":[{"id":8752,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8750\/revisions\/8752"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}