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
Be the first one to answer
Add answer anonymously...
Amazon Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter