Shopping Offers

In LeetCode Store, there are n items to sell. Each item has a price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.

You are given an integer array price where price[i] is the price of the i^th item, and an integer array needs where needs[i] is the number of pieces of the i^th item you want to buy.

You are also given an array special where special[i] is of size n + 1, where special[i][j] is the number of pieces of the j^th item in the i^th offer and special[i][n] (the last integer in the array) is the price of the i^th offer.

Return the lowest price you have to pay for exactly the items as given in needs, where you could make optimal use of the special offers. You are not allowed to buy more items than you want, even if that would lower the overall price. You could use any of the special offers as many times as you want.

Example 1
Inputprice = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2]
Output14
You can use the second special offer for 1A and 2B at $10, then buy 2A individually for $4, for a total of $14.
Example 2
Inputprice = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1]
Output11
You can use the first special offer for 1A and 1B at $4, then buy 1B and 1C individually for $3 and $4, for a total of $11.

Constraints

  • n == price.length == needs.length
  • 1 <= n <= 6
  • 0 <= price[i], needs[i] <= 10
  • 1 <= special.length <= 100
  • special[i].length == n + 1
  • 0 <= special[i][j] <= 50
  • The input is generated that at least one of special[i][j] is non-zero for 0 <= j <= n - 1.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate