diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2105.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2105.java new file mode 100644 index 0000000..cc4de8e --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2105.java @@ -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; + } +}