Java Hackerrank Mark和Toys质疑我的解决方案不适用于大型输入测试用例
下面是hackerrank的问题陈述 马克和简生了第一个孩子后非常高兴。他们的儿子喜欢玩具,所以马克想买一些。有许多不同的玩具摆在他面前,标有它们的价格。马克只有一定数量的钱可以花,他想最大限度地利用这笔钱购买玩具 给出一份价格清单和消费金额,马克最多能买多少玩具?例如,如果价格=[1,2,3,4]且马克有k=7可消费,他可以以6或7个货币单位购买物品[1,2,3],或[3,4]。他会选择第一组3个项目 下面是我为这个问题编写的代码,它涉及回溯技术Java Hackerrank Mark和Toys质疑我的解决方案不适用于大型输入测试用例,java,Java,下面是hackerrank的问题陈述 马克和简生了第一个孩子后非常高兴。他们的儿子喜欢玩具,所以马克想买一些。有许多不同的玩具摆在他面前,标有它们的价格。马克只有一定数量的钱可以花,他想最大限度地利用这笔钱购买玩具 给出一份价格清单和消费金额,马克最多能买多少玩具?例如,如果价格=[1,2,3,4]且马克有k=7可消费,他可以以6或7个货币单位购买物品[1,2,3],或[3,4]。他会选择第一组3个项目 下面是我为这个问题编写的代码,它涉及回溯技术 import java.util.ArrayL
import java.util.ArrayList;
import java.util.Collections;
public class MarkAndToys {
static ArrayList<Integer> possibleSolutions = new ArrayList<>();
static boolean findSolution(int[] prices,int amount,int left,int length,int items){
// Base case: if whole array was iterated and amount is >=0 then we got a solution
if(left >= length){
if(amount>=0){
possibleSolutions.add(items);
return true;
}
return false;
}
// Key idea: prices[left] is chosen or it is not.
// Deal with prices[left], letting recursion
// deal with all the rest of the array.
// Recursive call trying the case that prices[left] is chosen --
// subtract it from amount in the call.
if (findSolution(prices,amount-prices[left],left+1,length,items+1)) return true;
// Recursive call trying the case that prices[left] is not chosen.
if (findSolution(prices,amount,left+1,length,items)) return true;
// If neither of the above worked, it's not possible.
return false;
}
// Complete the maximumToys function below.
static int maximumToys(int[] prices, int k) {
if(findSolution(prices,k,0,prices.length,0)){
//if solutions are found then return maximum of them
return Collections.max(possibleSolutions);
}
return 0;
}
public static void main(String[] args) {
System.out.println(maximumToys(new int[]{1,12,5,111,200,1000,10}, 50));
}
}
import java.util.ArrayList;
导入java.util.Collections;
公营商标及玩具{
静态ArrayList possibleSolutions=新ArrayList();
静态布尔查找解决方案(int[]价格、int金额、int左、int长度、int项目){
//基本情况:若对整个数组进行迭代,并且数量>=0,那个么我们得到了一个解决方案
如果(左>=长度){
如果(金额>=0){
可能的解决方案。添加(项目);
返回true;
}
返回false;
}
//关键思想:价格[左]是选择还是不选择。
//处理价格[左],让递归
//处理阵列的所有其余部分。
//在选择价格[左]的情况下尝试递归调用--
//从通话中的金额中减去。
if(findSolution(价格、金额价格[左]、左+1、长度、项目+1))返回true;
//递归调用尝试未选择价格[左]的情况。
if(findSolution(价格、金额、左+1、长度、项目))返回true;
//如果上述两项都不起作用,那就不可能了。
返回false;
}
//完成下面的maximumToys功能。
静态整数最大玩具(整数[]价格,整数k){
if(findSolution(prices,k,0,prices.length,0)){
//如果找到解决方案,则返回最大值
返回集合.max(可能的解决方案);
}
返回0;
}
公共静态void main(字符串[]args){
System.out.println(maximumToys(新的int[]{1,12,5112001000,10},50));
}
}
这似乎工作正常:
// Complete the maximumToys function below.
static int maximumToys(int[] prices, int k) {
Arrays.sort(prices);
int sum = 0;
int index = 0;
for(int i = 0; i < prices.length; i++) {
sum+=prices[i];
index = i;
if(sum > k) {
break;
}
}
return index;
}
//完成下面的maximumToys函数。
静态整数最大玩具(整数[]价格,整数k){
数组。排序(价格);
整数和=0;
int指数=0;
对于(int i=0;ik){
打破
}
}
收益指数;
}
你能分享问题的联系吗?你的算法会导致指数时间复杂度,它相当昂贵。你只需要想出一个更好的解决方案。链接到问题提示以获得更有效的解决方案:首先尝试对价格
数组进行排序。是否可以为这个问题实施有效的回溯方法?是的,但我想使用回溯技术。很高兴看到人们在这里进行竞争性编程。