2021年5月1日星期六

Google Kickstart round-A 2020 Allocation

Problem There are N houses for sale. The i-th house costs Ai dollars to buy. You have a budget of B dollars to spend.

What is the maximum number of houses you can buy?

Time limit: 15 seconds per test set. Memory limit: 1GB. 1 ≤ T ≤ 100. 1 ≤ B ≤ 105. 1 ≤ Ai ≤ 1000, for all i.

I am trying to first sort the prices of the houses and then deducting the budget by the lowest priced house left in our array till all the budget is used. The interface is saying that I have a runtime error but with the sample test cases I ran on my ide it is not giving such thing. What can I do to solve this runtime error

import java.util.Scanner;

import java.util.Arrays;

class Houses{

public static void main(String []args){        Scanner sc = new Scanner(System.in);      int T = sc.nextInt();      int N, B;      int []arr;      int []result = new int[T];        for(int i=0; i<T; i++){          N = sc.nextInt();          B = sc.nextInt();          arr = new int[N];                for(int j=0; j<N; j++)              arr[j] = sc.nextInt();                int houses=0, k=0;          Arrays.sort(arr);          while(B>=arr[k]){             B = B-arr[k];             k++;             houses++;          }          result[i] = houses;      }      for(int i=0; i<T; i++){          System.out.println("Case #" + (i+1) + ": " + result[i]);      }  }  

}

https://stackoverflow.com/questions/67352622/google-kickstart-round-a-2020-allocation May 02, 2021 at 11:03AM

没有评论:

发表评论