Skip to content

Latest commit

 

History

History
175 lines (120 loc) · 5.8 KB

0638._Shopping_Offers.md

File metadata and controls

175 lines (120 loc) · 5.8 KB

638. Shopping Offers

难度: Medium

刷题内容

原题连接

内容描述

In LeetCode Store, there are some kinds of 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 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.

Example 1:
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:
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:
There are at most 6 kinds of items, 100 special offers.
For each item, you need to buy at most 6 of them.
You are not allowed to buy more items than you want, even if that would lower the overall price.

解题方案

思路 1 - 时间复杂度: O(n^(k+1) * m * n)- 空间复杂度: O(n^(k+1))******

DFS, beats 100%, 参考sl3635

时间复杂度n^(k+1) * m * n ( n kinds of items n<=6, number of each items k<=6, m special offers m<=100) There are only n^(k+1) different states (Quantity of every items can be 0 to 6, there are no more than 6 items), for each states, we go through each offer, the complexity is O(mn),引用于KakaHiguain

class Solution:
    def shoppingOffers(self, price, special, needs):
        """
        :type price: List[int]
        :type special: List[List[int]]
        :type needs: List[int]
        :rtype: int
        """
        self.dp = {}
        return self.dfs(price, special, needs)
    
    def dfs(self, price, special, needs):
        if tuple(needs) in self.dp: # Memorization
            return self.dp[tuple(needs)]
        
        cost = sum(x*y for x, y in zip(needs, price)) # Don't take offers
        
        for offer in special: # Take one offer
            for i, need in enumerate(needs):
                if need < offer[i]: # Make sure it can take at least one offer
                    break
            else: # no break in thie for loop
                new_needs = [need - offer[i] for i, need in enumerate(needs)]
                cost = min(cost, offer[-1] + self.dfs(price, special, new_needs))
        self.dp[tuple(needs)] = cost
        return cost

另外一种比较简洁的写法,beats 57.42%, 参考tlp108

class Solution:
    def shoppingOffers(self, price, special, needs):
        """
        :type price: List[int]
        :type special: List[List[int]]
        :type needs: List[int]
        :rtype: int
        """
        cache = {}
        def dfs(cur_needs):
            cost = sum(x*y for x, y in zip(cur_needs, price)) #cost without special
            for offer in special:
                new_needs = [cur_needs[i] - offer[i] for i in range(len(needs))]
                if min(new_needs) >= 0: # skip deals that exceed needs
                    # get check the dictionary first for result, otherwise perform dfs.
                    cost = min(cost, cache.get(tuple(new_needs), dfs(new_needs)) + offer[-1]) 
            cache[tuple(cur_needs)] = cost
            return cost
        return dfs(needs)

思路 2 - 时间复杂度: O(n^(k+1) * m * n)- 空间复杂度: O(n^(k+1))******

迭代, beats 16.77%

参考johnsmith94085123

class Solution:
    def shoppingOffers(self, price, special, needs):
        """
        :type price: List[int]
        :type special: List[List[int]]
        :type needs: List[int]
        :rtype: int
        """
        min_cost = sum(x*y for x, y in zip(needs, price))
        no_special = tuple([0]*len(special))
        plans_created, q = set([no_special]), collections.deque([(no_special, needs, min_cost)])
        while q:
            (prev_plan, prev_needs, prev_cost) = q.pop()
            # create new plans
            for i in range(len(special)):
                needs, plan = prev_needs[:], list(prev_plan)
                plan[i] += 1
                if tuple(plan) in plans_created: 
                    continue
                plans_created.add(tuple(plan))
                needs = list(map(lambda j: needs[j]-special[i][j], range(len(price))))
                if min(needs) < 0: 
                    continue
                cost = prev_cost + special[i][-1] - sum([x*y for (x, y) in zip(special[i], price)])
                min_cost = min(min_cost, cost)
                q.appendleft((tuple(plan), needs, cost))
        return min_cost