forked from rising-entropy/Leetcode-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatering_Plants.java
More file actions
28 lines (27 loc) · 888 Bytes
/
Watering_Plants.java
File metadata and controls
28 lines (27 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public static void main(String[] args) {
Solution sol = new Solution();
int plants[] = {2,2,3,3};
int capacity = 5;
System.out.println(sol.wateringPlants(plants, capacity));
}
public int wateringPlants(int[] plants, int capacity) {
int steps = 0;
int walk_back = 0;
int n = plants.length;
int ncap = capacity;
for (int i = 0; i < n; i++) {
System.out.println("1 Step walked for plant " + i);
steps += 1;
ncap -= plants[i];
if (i != (n - 1) && ncap < plants[i + 1]) {
int s = (i + 1);
walk_back += 2 * s;
System.out.println(walk_back+" Step walked to river for plant " + i+1);
ncap = capacity;
}
}
steps += walk_back;
return steps;
}
}