leetcode 638. Shopping Offers 最佳购物优惠+十分典型深度优先遍历DFS
2017-12-21 09:53
531 查看
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 2and2and5 respectively.
In special offer 1, you can pay 5for3Aand0BInspecialoffer2,youcanpay5for3Aand0BInspecialoffer2,youcanpay10 for 1A and 2B.
You need to buy 3A and 2B, so you may pay 10 for 1A and 2B (special offer #2), and10 for 1A and 2B (special offer #2), and4 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,and2,and3 for B, 4forC.Youmaypay4forC.Youmaypay4 for 1A and 1B, and 9for2A,2Band1C.Youneedtobuy1A,2Band1C,soyoumaypay9for2A,2Band1C.Youneedtobuy1A,2Band1C,soyoumaypay4 for 1A and 1B (special offer #1), and 3for1B,3for1B,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,
c833
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.
本题题意很简单,就是在一系列的购物优惠中选取最佳的策略,以获取少的花费
直接递归即可,很棒的做法
代码如下:
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 2and2and5 respectively.
In special offer 1, you can pay 5for3Aand0BInspecialoffer2,youcanpay5for3Aand0BInspecialoffer2,youcanpay10 for 1A and 2B.
You need to buy 3A and 2B, so you may pay 10 for 1A and 2B (special offer #2), and10 for 1A and 2B (special offer #2), and4 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,and2,and3 for B, 4forC.Youmaypay4forC.Youmaypay4 for 1A and 1B, and 9for2A,2Band1C.Youneedtobuy1A,2Band1C,soyoumaypay9for2A,2Band1C.Youneedtobuy1A,2Band1C,soyoumaypay4 for 1A and 1B (special offer #1), and 3for1B,3for1B,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,
c833
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.
本题题意很简单,就是在一系列的购物优惠中选取最佳的策略,以获取少的花费
直接递归即可,很棒的做法
代码如下:
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; class Solution { public: int shoppingOffers(vector<int>& p, vector<vector<int>>& sp, vector<int>& need) { int minsum = 0; for (int i = 0; i < p.size(); i++) minsum += p[i] * need[i]; for (auto s : sp) { bool can = true; for (int i = 0; i < need.size(); i++) { if (need[i] < s[i]) { can = false; break; } } if (can) { for (int i = 0; i < need.size(); i++) need[i] -= s[i]; minsum = min(minsum, s[need.size()] + shoppingOffers(p, sp, need)); for (int i = 0; i < need.size(); i++) need[i] += s[i]; } } return minsum; } };
相关文章推荐
- leetcode 695. Max Area of Island 岛屿的最大面积 + 十分典型的深度优先遍历DFS
- leetcode 464. Can I Win 100相加游戏 + 一个十分棒的深度优先遍历DFS的做法
- leetcode 491. Increasing Subsequences所有的递增序列 + 一个典型的深度优先遍历DFS的做法
- leetcode 329. Longest Increasing Path in a Matrix 矩阵中寻找最长递增序列 + 一个典型的深度优先遍历DFS的做法
- leetcode 547. Friend Circles 寻找图的环的数量 + 典型的深度优先遍历DFS
- leetcode 399. Evaluate Division 等式求解+典型的DFS深度优先遍历
- leetcode 543. Diameter of Binary Tree 最长树的片段 + 深度优先遍历DFS
- LeetCode 之 DFS 深度优先遍历
- leetcode 749. Contain Virus 消灭病毒建立墙 + 深度优先遍历DFS
- leetcode 97. Interleaving String(字符串交错出现) DFS深度优先遍历 + 很明显很经典的DP动态规划做法
- leetcode 513. Find Bottom Left Tree Value 最左边的值 + 一个简单的DFS深度优先遍历
- leetcode 572. Subtree of Another Tree 子树判断 + 深度优先遍历DFS
- leetcode 417. Pacific Atlantic Water Flow可以到达两边的点+经典DFS深度优先遍历做法
- leetcode 222. Count Complete Tree Nodes 计算满二叉树的节点数量 + DFS深度优先遍历 + 公式计算
- leetcode 282. Expression Add Operators 任意添加运算符计算结果 +深度优先遍历DFS
- leetcode 650. 2 Keys Keyboard 最小复制粘贴次数 + 深度优先遍历DFS
- leetcode 139. Word Break 深度优先遍历DFS按照index递归搜索 + 很棒的动态规划DP做法
- leetcode 397. Integer Replacement 二分查找 + 深度优先遍历DFS
- leetcode 529. Minesweeper 扫雷游戏 + 经典的DFS深度优先遍历
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树BST的最近公共祖先LCA + 深度优先遍历DFS