638. Shopping Offers

https://leetcode.com/problems/shopping-offers/description/

解题思路:

  1. 首先确定其停止(返回)条件
  2. 确定其深度搜索范围
  3. 返回当前值

class Solution {
Map, Integer> map = new HashMap<>();
public int shoppingOffers(List price, List> special, List needs) {

    if(map.containsKey(needs)) return map.get(needs);
    int sum = Integer.MAX_VALUE;
    for(List offer : special){
        List reducedNeeds = new ArrayList<>();
        boolean validSpecial = true;
        for(int i = 0; i < needs.size(); i++){
            reducedNeeds.add( needs.get(i) - offer.get(i));
            if(reducedNeeds.get(i) < 0){
                validSpecial = false;
                break;
            }
        }
        if(validSpecial){
            sum = Math.min(sum, offer.get(offer.size() - 1) + shoppingOffers(price, special, reducedNeeds));
        }
    }
    int normal = 0;
    for(int i = 0; i < needs.size(); i++){
        normal += price.get(i) * needs.get(i);
    }
    sum = Math.min(sum, normal);
    map.put(needs, sum);
    return sum;
}

}

你可能感兴趣的:(638. Shopping Offers)