|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * 638. Shopping Offers |
| 8 | + * |
| 9 | + * In LeetCode Store, there are some kinds of items to sell. Each item has a price. |
| 10 | + * However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. |
| 11 | + * You are given the each item's price, a set of special offers, |
| 12 | + * and the number we need to buy for each item. |
| 13 | + * The job is to output the lowest price you have to pay for exactly certain items as given, |
| 14 | + * where you could make optimal use of the special offers. |
| 15 | + * Each special offer is represented in the form of an array, |
| 16 | + * the last number represents the price you need to pay for this special offer, |
| 17 | + * other numbers represents how many specific items you could get if you buy this offer. |
| 18 | + * You could use any of special offers as many times as you want. |
| 19 | +
|
| 20 | + Example 1: |
| 21 | + Input: [2,5], [[3,0,5],[1,2,10]], [3,2] |
| 22 | + Output: 14 |
| 23 | + Explanation: |
| 24 | + There are two kinds of items, A and B. Their prices are $2 and $5 respectively. |
| 25 | + In special offer 1, you can pay $5 for 3A and 0B |
| 26 | + In special offer 2, you can pay $10 for 1A and 2B. |
| 27 | + You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A. |
| 28 | +
|
| 29 | + Example 2: |
| 30 | + Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1] |
| 31 | + Output: 11 |
| 32 | + Explanation: |
| 33 | + The price of A is $2, and $3 for B, $4 for C. |
| 34 | + You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. |
| 35 | + 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. |
| 36 | + You cannot add more items, though only $9 for 2A ,2B and 1C. |
| 37 | +
|
| 38 | + Note: |
| 39 | + There are at most 6 kinds of items, 100 special offers. |
| 40 | + For each item, you need to buy at most 6 of them. |
| 41 | + You are not allowed to buy more items than you want, even if that would lower the overall price. |
| 42 | + */ |
| 43 | +public class _638 { |
| 44 | + /**reference: https://leetcode.com/articles/shopping-offers/#approach-1-using-recursion-accepted*/ |
| 45 | + public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) { |
| 46 | + return shopping(price, special, needs, 0); |
| 47 | + } |
| 48 | + |
| 49 | + public int shopping(List < Integer > price, List < List < Integer >> special, List < Integer > needs, int i) { |
| 50 | + if (i == special.size()) { |
| 51 | + return dot(needs, price); |
| 52 | + } |
| 53 | + ArrayList<Integer> clone = new ArrayList(needs); |
| 54 | + int j = 0; |
| 55 | + for (j = 0; j < special.get(i).size() - 1; j++) { |
| 56 | + int diff = clone.get(j) - special.get(i).get(j); |
| 57 | + if (diff < 0) break; |
| 58 | + clone.set(j, diff); |
| 59 | + } |
| 60 | + if (j == special.get(i).size() - 1) { |
| 61 | + return Math.min(special.get(i).get(j) + shopping(price, special, clone, i), shopping(price, special, needs, i + 1)); |
| 62 | + } else { |
| 63 | + return shopping(price, special, needs, i + 1); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public int dot(List<Integer> a, List<Integer> b) { |
| 68 | + int sum = 0; |
| 69 | + for (int i = 0; i < a.size(); i++) { |
| 70 | + sum += a.get(i) * b.get(i); |
| 71 | + } |
| 72 | + return sum; |
| 73 | + } |
| 74 | +} |
0 commit comments