[Swift]LeetCode638. 大禮包 | Shopping Offers

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-njidlwmw-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

In LeetCode Store, there are some kinds of items to sell. Each item has a price.git

However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.github

You are given the each item's price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers.數組

Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.微信

You could use any of special offers as many times as you want.this

Example 1:spa

Input: [2,5], [[3,0,5],[1,2,10]], [3,2]
Output: 14
Explanation: 
There are two kinds of items, A and B. Their prices are $2 and $5 respectively. 
In special offer 1, you can pay $5 for 3A and 0B
In special offer 2, you can pay $10 for 1A and 2B. 
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A. 

Example 2:code

Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
Output: 11
Explanation: 
The price of A is $2, and $3 for B, $4 for C. 
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. 
You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C. 
You cannot add more items, though only $9 for 2A ,2B and 1C. 

Note:orm

  1. There are at most 6 kinds of items, 100 special offers.
  2. For each item, you need to buy at most 6 of them.
  3. You are not allowed to buy more items than you want, even if that would lower the overall price.

在LeetCode商店中, 有許多在售的物品。htm

然而,也有一些大禮包,每一個大禮包以優惠的價格捆綁銷售一組物品。

現給定每一個物品的價格,每一個大禮包包含物品的清單,以及待購物品清單。請輸出確切完成待購清單的最低花費。

每一個大禮包的由一個數組中的一組數據描述,最後一個數字表明大禮包的價格,其餘數字分別表示內含的其餘種類物品的數量。

任意大禮包可無限次購買。

示例 1:

輸入: [2,5], [[3,0,5],[1,2,10]], [3,2]
輸出: 14
解釋: 
有A和B兩種物品,價格分別爲¥2和¥5。
大禮包1,你能夠以¥5的價格購買3A和0B。
大禮包2, 你能夠以¥10的價格購買1A和2B。
你須要購買3個A和2個B, 因此你付了¥10購買了1A和2B(大禮包2),以及¥4購買2A。

示例 2:

輸入: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
輸出: 11
解釋: 
A,B,C的價格分別爲¥2,¥3,¥4.
你能夠用¥4購買1A和2B,也能夠用¥9購買2A,2B和1C。
你須要買1A,2B和1C,因此你付了¥4買了1A和1B(大禮包1),以及¥3購買1B, ¥4購買1C。
你不能夠購買超出待購清單的物品,儘管購買大禮包2更加便宜。

說明:

  1. 最多6種物品, 100種大禮包。
  2. 每種物品,你最多隻須要購買6個。
  3. 你不能夠購買超出待購清單的物品,即便更便宜。

Runtime: 76 ms
Memory Usage: 18.9 MB
 1 class Solution {
 2     func shoppingOffers(_ price: [Int], _ special: [[Int]], _ needs: [Int]) -> Int {
 3         var needs = needs
 4         var res:Int = 0
 5         var n:Int = price.count
 6         for i in 0..<n
 7         {
 8             res += price[i] * needs[i]
 9         }
10         for offer in special        
11         {
12             var isValid:Bool = true
13             for j in 0..<n
14             {
15                 if needs[j] - offer[j] < 0
16                 {
17                     isValid = false                    
18                 }
19                 needs[j] -= offer[j]
20             }
21             if isValid
22             {
23                 res = min(res, shoppingOffers(price, special, needs) + offer.last!)
24             }
25             for j in 0..<n
26             {
27                 needs[j] += offer[j]
28             }
29         }
30         return res
31     }
32 }
相關文章
相關標籤/搜索