Link: https://www.nowcoder.com/questionTerminal/893ad8352e21488f8a7c43e1c5bb34e1
Source: Niu Ke Net
Niuniu after the Google Girl hackathon, take a taxi back to Niu Jia Zhuang.
Niuniu needs to pay the taxi driver the fare s yuan. Niuniu has a total of n coins, the first I coin value of p[i] yuan.
Niuniu wants to choose as many coins as possible, so that its total value is enough to pay the S-yuan fare (that is, greater than or equal to s).
But if one or more coins were removed from the coins paid by Niuniu, the taxi driver would not accept the remaining coins in total value enough to pay the fare. For example: Niuniu use value for 2,5,7 coins to pay s=11 fare, taxi driver is not acceptable, because the value of 2 of the coin can be removed.
Niuniu wanted to choose the largest number of coins to make the total value sufficient to pay the fare and the taxi driver was able to accept it.
Niuniu wants you to help her figure out how many coins to pay.
Enter a description:
The input includes two lines, the first row consists of two positive integers n and s (1 <= n <=, 1 <= S <= 1000), indicating the number of coins for Niuniu and the fares to be paid. The
second line consists of n positive integers p[i] (1 <= p[i] <= 100), representing the value of the first coin.
ensure that the sum of the n coins of Niuniu is greater than or equal to S.
Output Description:
Output An integer that indicates the maximum number of coins that Niuniu can pay.
Example 1
input
5 9
4 1 3 5 4
Output
3
"Debugging several times, always can not reach the 100% pass rate, the original idea only stop in ascending order array, not grasp the core of the idea"
"Be sure to think of the most complicated case: {1,1,1,2,3,5,20} fare is 19" then the final output is 1.
The elements are sorted first, and then joined in sequence, if the equality is returned directly, if the sum > m, the Record difference//Then from the current I to find the first priority to be deleted point,//to the maximum number, then definitely priority to delete the large value.
Sort it first.
Import Java.util.Arrays;
Import Java.util.Scanner;
public class main{public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in);
string[] mn = Scanner.nextline (). Split ("");
int n = integer.parseint (mn[0]);
int m = Integer.parseint (mn[1]);
Next read the information for each line, stored in the string mn, and separated by a space of int [] point = new Int[n];
for (int i = 0;i < n;i++) {Point[i] = Integer.parseint (Scanner.next ());
} System.out.print (Solve (point,m));
Scanner.close ();
public static int Solve (int[] point,int m) {arrays.sort (point);
int I=0,j;int sum =0;int cnt = 0;
for (i=0;i<point.length;i++) {sum+=point[i];
cnt++;
if (sum==m) return CNT;
else if (Sum > m) {int temp = SUM-M;
for (j = i;j>=0;j--) {if (temp >= point[j]) {cnt--;
TEMP-=POINT[J];
else if (temp <=0) {return cnt;//This must be added, or it will continue I for loop}}
} return CNT; }
}
"It's important to be familiar with the readings from the console."