class Solution {
public:
int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
// 双指针
int n = plants.size();
int i = 0, j = n - 1;
int s = 0;
int watera = capacityA, waterb = capacityB;
while(i < j) {
if(watera < plants[i]){
watera = capacityA;
s++;
}
watera -= plants[i ++ ];
if(waterb < plants[j]){
waterb = capacityB;
s++;
}
waterb -= plants[j -- ];
}
if(i == j) {
int mx = max(watera, waterb);
if(mx < plants[i]) {
s++;
}
}
return s;
}
};
【Daily Code】leetcode 2105.给植物浇水
最新推荐文章于 2024-08-25 17:36:33 发布