2024/11/5 灵茶题单 滑动窗口 相向双指针
This commit is contained in:
parent
a8f8e8d875
commit
6f85ee5738
|
@ -0,0 +1,34 @@
|
||||||
|
package com.cool.ling_cha_mount.sliding_windows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/11/05/18:35
|
||||||
|
* @Description: 2105. 给植物浇水 II
|
||||||
|
*/
|
||||||
|
public class Num2105 {
|
||||||
|
public int minimumRefill(int[] plants, int capacityA, int capacityB) {
|
||||||
|
int capA = capacityA;
|
||||||
|
int capB = capacityB;
|
||||||
|
int left = 0;
|
||||||
|
int right = plants.length - 1;
|
||||||
|
int res = 0;
|
||||||
|
while (left < right) {
|
||||||
|
if (capA < plants[left]) {
|
||||||
|
capA = capacityA;
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
if (capB < plants[right]) {
|
||||||
|
capB = capacityB;
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
capA -= plants[left++];
|
||||||
|
capB -= plants[right--];
|
||||||
|
}
|
||||||
|
if (left == right && Math.max(capA, capB) < plants[left]) {
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue