-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
65 lines (45 loc) · 1.26 KB
/
Solution.java
File metadata and controls
65 lines (45 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package hackrank.algorithm.greedy.toys;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Scanner;
/**
* Mark and Toys
*
* @see https://www.hackerrank.com/challenges/mark-and-toys
*/
public class Solution {
public static void main(String[] args) {
ToyStore toyStore = readInput(System.in);
Arrays.sort(toyStore.prices);
int spend = 0;
int numberToys = 0;
for (int price : toyStore.prices) {
spend += price;
if (spend > toyStore.budget) {
break;
} else {
numberToys++;
}
}
System.out.println(numberToys);
}
public static ToyStore readInput(InputStream input) {
Scanner scanner = new Scanner(input);
int numberPrices = scanner.nextInt();
int budget = scanner.nextInt();
int[] prices = new int[numberPrices];
for (int i = 0; i < numberPrices; i++) {
prices[i] = scanner.nextInt();
}
scanner.close();
return new ToyStore(budget, prices);
}
}
class ToyStore {
public int budget;
public int[] prices;
public ToyStore(int budget, int[] prices) {
this.budget = budget;
this.prices = prices;
}
}