赞
踩
根据题目内容,使用双指针从左右两边同时向中间移动,模拟浇水过程即可。
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; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。