Leetcode Problem 2105. Watering Plants II

2105. Watering Plants II

Leetcode Solutions

Two Pointer Approach

  1. Initialize two pointers, start for Alice starting at 0 and end for Bob starting at n - 1.
  2. Initialize aliceCC and bobCC to capacityA and capacityB respectively, representing the current capacity of their watering cans.
  3. Initialize a counter count to keep track of the number of refills.
  4. Loop while start is less than or equal to end.
    • If start equals end, check who should water the plant and if a refill is needed.
    • Otherwise, Alice waters the plant at start and Bob waters the plant at end if they have enough water, refilling if necessary.
    • Increment start and decrement end.
  5. Return the count of refills.
UML Thumbnail

Simulate Watering Process

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...