Amazon Interview Question. Can anyone solve this?
Anonymous User
362

John goes to IB office on N days on his bike which consumes exactly 1 litre petrol every day but his bike can only store at max B Litres of petrol. You are given petrol prices for all N days as integer array A.
Find the minimum cost that could occur to John if he chooses optimal quantity to refuel everyday.
NOTE:
• Refueling station is very very close to Rishik's home. So, he can reach there from home at 0 petrol. But, he cannot go to office with 0 fuel.
• Initially, the bike has 0 fuel.

Example Input
Input 1:
A = [5, 6, 3, 4, 2, 1]
B = 2
Input 2:
A = [7, 3, 3, 5, 2]
B = 3
Example Output
Output 1:
19
Output 2:
18

Example Explanation
Explanation 1:
Quantity of petrol filled each day = [2, 0, 2, 0, 1, 1]. This gives minimum cost.
Explanation 2:
Quantity of petrol filled each day = [1, 1, 2, 0, 1]. This gives minimum cost.

public class Solution {
public int solve(int[] A, int B) {
}
}

Comments (7)