the proposed answer seems wrong, test 4 should return 24 and test 5 should return 19
omg thank you! Test cases had me so confused thinking I got it wrong. Test case 5 should actually return 27.
No, test case 5 is clearly 21 and is correct.
Bowls = 3 4 1 2 6
Capacity = 6
First you start at the bowl (position -1)
You walk to first bowl (1 step) then your ladle only has 3 left.
You walk back and refill - 1 step. Then you walk to second bowl - 2 steps.
Then you walk to third bowl - 1 step.
Then you refill. 3 steps. Then you walk to fourth bowl - 4 steps.
Then you refill - another 4 steps.
Then 5 steps to fifth bowl.
21 steps total.
I reckon my code is easier to follow because it reads along with the problem description better.
int min_steps(std::vector bowls, int capacity) {
int position = -1;
int ladle = 0;
int distance = 0;
for (int i = 0; i < bowls.size(); ++i) {
if (ladle < bowls[i]) {
distance += position - (-1);
position = -1;
ladle = capacity;
}
distance += i - position;
position = i;
ladle -= bowls[i];
}
return distance;
}