力扣题目:给植物浇水 II
题目链接: 2105.给植物浇水 II
题目描述
代码思路
根据题目内容,使用双指针从左右两边同时向中间移动,模拟浇水过程即可。
代码纯享版
class Solution {public int minimumRefill(int[] plants, int capacityA, int capacityB) {int left = 0, right = plants.length - 1;int left_water = capacityA, right_water = capacityB;int sum = 0;while(left <= right){if(left < right){if(left_water < plants[left]){sum++;left_water = capacityA - plants[left];}else left_water -= plants[left];if(right_water < plants[right]){sum++;right_water = capacityB - plants[right];}else right_water -= plants[right];}else{if(Math.max(left_water, right_water) < plants[left]) sum++;}left++;right--;}return sum;}
}