给你一部分钱和一些不同价钱的商品,如何在最多买K件商品的情况下尽可能多的花掉手里的钱。
举例:口袋里的钱数: 10; K=2 产品价格: [3, 6, 8, 7, 9] 输出 3, 7

Backtracking:

 package BuyGoods;
import java.util.*; public class Solution {
static int minRemain = 0; public ArrayList<Integer> optimize(int money, int[] prices, int k) {
ArrayList<Integer> result = new ArrayList<Integer>();
ArrayList<Integer> path = new ArrayList<Integer>();
minRemain = money;
helper(result, path, money, prices, 0, k);
return result;
} public void helper(ArrayList<Integer> result, ArrayList<Integer> path, int remain, int[] prices, int pos, int times) {
if (remain < 0 || times<0) return;
if (remain < minRemain) {
minRemain = remain;
result.clear();
result.addAll(path);
}
for (int i=pos; i<prices.length; i++) {
path.add(prices[i]);
helper(result, path, remain-prices[i], prices, i+1, times-1);
path.remove(path.size()-1);
} } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
ArrayList<Integer> result = sol.optimize(10, new int[]{7,8,1,6,9}, 3);
System.out.println(result);
} }

G面经prepare: BuyGoods的更多相关文章

  1. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  2. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  3. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  4. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  5. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  6. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  7. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

  8. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

  9. G面经Prepare: Valid Preorder traversal serialized String

    求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...

随机推荐

  1. the essence of the internet idea

    Computer Systems A Programmer's Perspective Second Edition Of course, we are glossing over many diff ...

  2. 【php学习】mysql数据库操作

    //建立数据库连接,数据库地址127.0.0,用户名root,密码root $dbConn = mysql_connect('127.0.0.1', 'root', 'root'); mysql_qu ...

  3. 在使用EFCodeFirst中出现类型“System.Data.Objects.ObjectContext”在未被引用的程序集中定义的解决方案

    我安装了EF4.1版本,并在一个项目中映射一个数据库并生成了EF的MODEL实体层 测试:在Default.aspx页面上加了个GridView控件,后台进行绑定 using System; usin ...

  4. 低功耗蓝牙4.0BLE编程-nrf51822开发(7)-SDP服务发现协议

    SDP的全称是Service Discovery Protocol,中文是服务发现协议.SDP(服务发现协议)是蓝牙协议体系中的核心协议,是蓝牙系统重要组成部分,是所有用户模式的基础.在蓝牙系统中.客 ...

  5. UISearchBar cover first cell of UITableView

    1.相当重要的是 tableView.tableHeaderView = searchBar; 这一句一定要在 UIViewController viewDidLoad 的时候执行,否则就会出现 se ...

  6. 转 创建 JavaScript XML 文档注释

    http://www.cnblogs.com/chenxizhang/archive/2009/07/12/1522058.html 如何:创建 JavaScript XML 文档注释 Visual ...

  7. css背景图片定位练习(二): background-position的百分比

    background-position:x y; 百分比定位并不能直观的看出来,需要通过计算. background-position百分比计算公式: (容器宽度—背景图片的宽度)*x%=xpx(容器 ...

  8. 让Eclipse不格式化数组或某段代码

    用过eclipse ctrl+shit+f的人肯定都感觉eclipse这个功能很爽. 但对于数组,有时候就不是这样了. 比如在opengl中定义一些顶点信息: int one = 0x010000; ...

  9. java简单优化和编写规范,自己总结的。

    1.永远不要比较两个浮点数是否相等.它是不安全的.详情google. 2.尽量使用StringBuffer代替String. 3.final类会提高很多效率. 4.try-catch 不应该用来控制程 ...

  10. Caused by: java.lang.OutOfMemoryError: PermGen space.

    现在eclipse需要加载4个项目同时运行了,所以当服务启动的时候,出现Caused by: java.lang.OutOfMemoryError: PermGen space.空间不足错误,我说一下 ...