G面经prepare: BuyGoods
给你一部分钱和一些不同价钱的商品,如何在最多买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的更多相关文章
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- 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 ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- 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 ...
- G面经Prepare: Valid Preorder traversal serialized String
求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...
随机推荐
- sshd调优
sshd调优:禁用dns查找,加快速度在sshd_config中设置:UseDNS no禁用root登录:建立普通用户在sshd_config中设置PermitRootLogin no以上设置需要重启 ...
- javaWeb总结——session
一.session简单介绍 在web开发中,服务器为每个用户浏览器创建一个会话对象,即session对象.一个浏览器独占一个session对象.因此,在需要保护用户数据时,服务器程序可以把用户数据写到 ...
- Nginx部署ThinkPHP项目的办法
thinkphp config配置: ', //URL模式 nginx rewrite配置: location / { if (!-e $request_filename) { rewrite ^(. ...
- quanpailie quanbianli
#include<stdio.h>char data[5]={'a','b','c','d','e'};int vist[5]={0};char step[5]={0};char bu[5 ...
- JAVA的覆盖、继承和多态的详细解说.this和super的用法
1. 继承: (1)子类的构造方法一定会调用父类的构造方法. (2)任何子类构造方法第一行肯定是this();或者super();两个择一. this();调用本类的其它构造方法.(传递相应参数调用相 ...
- Dom4j
Dom4j http://baike.baidu.com/link?url=2XOnr06saKUd-9By1GyPxIolXMQhf_C-CnMFll_yhtR4m00i27zphbkI5-dGpw ...
- SQL server 2008数据库的备份与还原(转)
一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Server Management Studio → 数据库:Dsi ...
- 使用微信JS-SDK 实现 自定义 分享 功能
微信PC端点击页面,转发给朋友.
- 获得sql对应的binary
Declare @max Varbinary(max) select @Max = convert(Varbinary(max) , ' Select ' ) Print ' DECLARE @sql ...
- 【Android开发学习笔记】【高级】【随笔】插件化——资源加载
前言 上一节我们针对插件最基本的原理进行了一个简单的demo实现,但是由于插件的Context对象被宿主所接管,因此无法加载插件程序的资源.那么如何解决这个问题捏? 有人提出这样的方案:将apk中的资 ...