Amazon Interview Question
Software Engineer in TestsCountry: India
Interview Type: In-Person
The idea is good. I think both the arrays c[] and price[] should be initialized with values = Integer.MAX_VALUE and c[0] and price[0] should be 0. And I also feel that
if (i == q[j] && price[i] > p[j])
{
c[i] = 1;
price[i] = p[j];
}
need not be handled separately.
How to we define the optimal combination??? For example say quantity[] = 1,5,10
and corresponding price[] = 10,30,80 . Now if total quantity is 20. We have 3 options
1) 20 bags cost:200. 2)4 bags Cost 120 and 3.) 2bags and cost160 and many more possibilities. In the above defined options which gets priority 2 or 3???
Using DP we can solve this.
Code:
Here I have kept the price to be optimum and then the quantity.
- Dhass April 14, 2013