Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 19e669a

Browse files
committed
Add solution #2105
1 parent dc08cca commit 19e669a

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,748 LeetCode solutions in JavaScript
1+
# 1,749 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1612,6 +1612,7 @@
16121612
2101|[Detonate the Maximum Bombs](./solutions/2101-detonate-the-maximum-bombs.js)|Medium|
16131613
2103|[Rings and Rods](./solutions/2103-rings-and-rods.js)|Easy|
16141614
2104|[Sum of Subarray Ranges](./solutions/2104-sum-of-subarray-ranges.js)|Medium|
1615+
2105|[Watering Plants II](./solutions/2105-watering-plants-ii.js)|Medium|
16151616
2114|[Maximum Number of Words Found in Sentences](./solutions/2114-maximum-number-of-words-found-in-sentences.js)|Easy|
16161617
2115|[Find All Possible Recipes from Given Supplies](./solutions/2115-find-all-possible-recipes-from-given-supplies.js)|Medium|
16171618
2116|[Check if a Parentheses String Can Be Valid](./solutions/2116-check-if-a-parentheses-string-can-be-valid.js)|Medium|

solutions/2105-watering-plants-ii.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* 2105. Watering Plants II
3+
* https://leetcode.com/problems/watering-plants-ii/
4+
* Difficulty: Medium
5+
*
6+
* Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are
7+
* labeled from 0 to n - 1 from left to right where the ith plant is located at x = i.
8+
*
9+
* Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially
10+
* full. They water the plants in the following way:
11+
* - Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters
12+
* the plants in order from right to left, starting from the (n - 1)th plant. They begin watering
13+
* the plants simultaneously.
14+
* - It takes the same amount of time to water each plant regardless of how much water it needs.
15+
* - Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise,
16+
* they first refill their can (instantaneously) then water the plant.
17+
* - In case both Alice and Bob reach the same plant, the one with more water currently in his/her
18+
* watering can should water this plant. If they have the same amount of water, then Alice should
19+
* water this plant.
20+
*
21+
* Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the
22+
* ith plant needs, and two integers capacityA and capacityB representing the capacities of Alice's
23+
* and Bob's watering cans respectively, return the number of times they have to refill to water all
24+
* the plants.
25+
*/
26+
27+
/**
28+
* @param {number[]} plants
29+
* @param {number} capacityA
30+
* @param {number} capacityB
31+
* @return {number}
32+
*/
33+
var minimumRefill = function(plants, capacityA, capacityB) {
34+
let result = 0;
35+
let left = 0;
36+
let right = plants.length - 1;
37+
let waterA = capacityA;
38+
let waterB = capacityB;
39+
40+
while (left <= right) {
41+
if (left === right) {
42+
if (waterA >= waterB && waterA < plants[left]) result++;
43+
if (waterB > waterA && waterB < plants[right]) result++;
44+
break;
45+
}
46+
47+
if (waterA < plants[left]) {
48+
waterA = capacityA;
49+
result++;
50+
}
51+
waterA -= plants[left++];
52+
53+
if (waterB < plants[right]) {
54+
waterB = capacityB;
55+
result++;
56+
}
57+
waterB -= plants[right--];
58+
}
59+
60+
return result;
61+
};

0 commit comments

Comments
 (0)