簡體   English   中英

Hackerrank Mark 和 Toys 質疑我的解決方案不適用於大型輸入測試用例

[英]Hackerrank Mark and Toys Question my solution not working for large input testcases

以下是來自 hackerrank 的問題陳述

馬克和簡在有了第一個孩子后非常開心。 他們的兒子喜歡玩具,所以馬克想買一些。 他面前擺着許多不同的玩具,上面貼着價格。 馬克只有一定數額可以花,他想用這筆錢購買最多的玩具。

給定價格清單和消費金額,馬克最多可以購買多少玩具? 例如,如果價格 = [1,2,3,4] 並且馬克有 k=7 可以花費,他可以用 6 個貨幣單位購買物品 [1,2,3],或者用 7 個貨幣單位購買 [3,4]。 他會選擇第一組的 3 個項目。

下面是我為這個問題編寫的代碼,其中涉及回溯技術

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));
    }

}

這似乎工作正常:

// 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;

}
package Scanarios;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Toys {
    
public static void main(String[] args) {
        
        Toys t=new Toys();
        int[] a = {3,6,2,1,4,5};
        int q=1;
        int n=6;
        ArrayList<Integer> queries[]=new ArrayList[n];
        ArrayList<Integer> result=new ArrayList();
        
        for (int i = 0; i < n; i++) {
             queries[i] = new ArrayList<Integer>();
        }
        
        queries[0].add(10);
        queries[0].add(2);
        queries[0].add(2);
        queries[0].add(5);

        result=t.maximumToys(n,a,q,queries);
        System.out.println(result);
    }
    
     public ArrayList<Integer> maximumToys(int n, int a[], int q, ArrayList<Integer> queries[]) {
            
         ArrayList<Integer> arrlist=new ArrayList();
            for(int z=0;z<q;z++) {
                int[] arr=queries[z].stream().mapToInt(i -> i).toArray();
                int cost=arr[0];
                int k=arr[1];
                int count=0;
                int[] proxyarr=new int[n-1];
                proxyarr =removeBrokenPrice(a,arr,k);
                Arrays.sort(proxyarr);

                for(int i=0;i< proxyarr.length;i++) {                       
                        
                    cost -=proxyarr[i];
                    if(cost > 0) {
                        count++; }else {
                        break;
                    }
                }
                arrlist.add(count);
            }
            return arrlist;
          }
                 
     int[] removeBrokenPrice (int a[],int b[],int k){
        
         int count=0;
         for(int i=k;i <= b.length-1;i++) {
            
            for(int j=0;j<a.length;j++) {
                
                if(j==b[i]-1) {
                    a[j]=-1;
                    count++;
                }
            }
        }
         int[] proxyarr=new int[a.length-count];
         
         for(int i=0,j=0;i< a.length;i++)
         {
             if(a[i]==-1) {
                 continue;
             }else {
                 proxyarr[j++]=a[i];
             }
         }
         return proxyarr;
         
     }
     


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM