Loading... > https://leetcode.cn/problems/watering-plants/description/?envType=daily-question&envId=2024-05-02 名为中等实为简单,模拟一下即可。 ```c++ class Solution { public: int wateringPlants(std::vector<int>& plants, int capacity) { const int n = plants.size(); int steps = n, water = capacity; for (int i = 0; i < n; ++i) { if (water < plants[i]) steps += 2 * i, water = capacity; water -= plants[i]; } return steps; } }; ``` - 时间复杂度:$O(n)$ ,其中 $n$ 为 `plants` 的长度。 - 空间复杂度:$O(1)$ 。 最后修改:2024 年 05 月 08 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏