
Asked in Amazon
Minimum Cost to Buy Oranges Problem Statement
You are given a bag of capacity 'W' kg and a list 'cost' of costs for packets of oranges with different weights. Each element at the i-th position in the list indicates the price of a packet weighing (i+1) kg.
If an element at i-th position in 'cost' is -1, it means the (i+1) kg packet is unavailable.
Your task is to determine the minimum total cost to purchase exactly 'W' kg of oranges. If it is not possible to buy exactly W kg, return -1. Assume an infinite supply of any available packet type.
Example:
Input:
N = 5, W = 5
cost = [20, 10, 4, 50, -1]
Output:
14
Explanation:
Minimum cost to achieve 5 kg of oranges is using one 3 kg packet costing 4, and one 2 kg packet costing 10. Total = 4 + 10 = 14.
Constraints:
1 ≤ N ≤ 1000
1 ≤ W ≤ 1000
-1 ≤ cost[i] ≤ 1000000
- Time Limit: 1 sec

Find the minimum cost to purchase a specific weight of oranges given the cost of different weight packets.
Iterate through the list of costs and find the minimum cost to achieve the desired weight
Keep ...read more
Top Software Engineer Interview Questions Asked at Amazon
Interview Questions Asked to Software Engineer at Other Companies
Top Skill-Based Questions for Amazon Software Engineer


Reviews
Interviews
Salaries
Users

