public class LeetCode_2105 {
public static void main(String[] args) {
int[] plants = {2, 1, 1};
System.out.println(minimumRefill(plants, 2, 2));
}
public static int minimumRefill(int[] plants, int capacityA, int capacityB) {
int AliceCan = capacityA;
int BobCan = capacityB;
int left = 0;
int right = plants.length - 1;
int count = 0;
while (left < right) {
// check if Alice can't give water, then fill Alice's Can
if (plants[left] > AliceCan) {
AliceCan = capacityA;
count++;
}
AliceCan -= plants[left];
// check if Bob can't give water, then fill Bob's Can
if (plants[right] > BobCan) {
BobCan = capacityB;
count++;
}
BobCan -= plants[right];
left++;
right--;
}
// if dono ke paas paani kam hai, to count++
if (left == right && (AliceCan < plants[left] && BobCan < plants[right])) {
count++;
}
return count;
}
}
Thursday, February 10, 2022
Leet Code 2105 - Watering Plants II
Subscribe to:
Post Comments (Atom)
Diagonal Traversal
eg. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 6 11 16 2 7 12 3 8 4 Approach:...
-
I/P: 24 10 20 50 -1 60 -1 -1 30 70 -1 80 110 -1 120 -1 -1 90 -1 -1 40 100 -1 -1 -1 O/P: 10 20 30 40 50 60 70 80 90 100 110 120 import ja...
-
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ Given the sorted rotated array nums of unique elements, retu...
-
https://leetcode.com/problems/koko-eating-bananas/description/ Piles: 📦 📦 📦 📦 → 4 piles Hours: ⏰ ⏰ ⏰ ⏰ ⏰ ⏰ → 6 h...
No comments:
Post a Comment