leetcode 2105. 给植物浇水 II
题目如下
数据范围
示例
简单的模拟题注意数组长度为奇数的情况就行。
通过代码
class Solution {
public:
int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
int n = plants.size();
int count = 0,a = capacityA,b = capacityB;
for(int i = 0;i < n/2;i++){
if(a < plants[i]){
count++;
a = capacityA;
}
if(b < plants[n - i - 1]){
count++;
b = capacityB;
}
a -= plants[i];
b -= plants[n - i - 1];
}
if(n % 2 == 0)return count;
if(a >= b){
if(a < plants[n/2])return count + 1;
return count;
}
if(b < plants[n/2])return count + 1;
return count;
}
};