-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
54 lines (39 loc) · 1.15 KB
/
Solution.java
File metadata and controls
54 lines (39 loc) · 1.15 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
package hackrank.algorithm.greedy.priyanka;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* Priyanka and Toys Challenge
*
* @see https://www.hackerrank.com/challenges/priyanka-and-toys
*/
public class Solution {
public static void main(String[] args) {
List<Integer> weights = readInput(System.in);
System.out.println(calcMinUnits(weights));
}
public static int calcMinUnits(List<Integer> weights) {
Collections.sort(weights);
int units = 0;
int freeRange = -1;
for (int weight : weights) {
if (weight > freeRange) {
units++;
freeRange = weight + 4;
}
}
return units;
}
public static List<Integer> readInput(InputStream stream) {
Scanner scanner = new Scanner(stream);
int size = scanner.nextInt();
List<Integer> weights = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
weights.add(scanner.nextInt());
}
scanner.close();
return weights;
}
}