AmazonOA #2
Group Students
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Given each student's skill value, assign all students to some number of non-empty classes. Within each class, the difference between the highest and lowest skill values must not exceed maxSpread. Find the minimum number of classes; students in the same class can be chosen freely and need not be contiguous in the original array.
Input
The first line contains n maxSpread (1≤n≤100000, 0≤maxSpread≤10⁹), and the second line contains n skill values (1..10⁹).
Output
Output a single integer, the answer required by the problem.
Example 1
Input:
4 3
4 8 1 7
Output:
2
Example 2
Input:
3 0
5 5 5
Output:
1
Example 3
Input:
3 1
1 3 5
Output:
3
Example notes
Example 1 can be split into two classes, [1,4] and [7,8], with spans 3 and 1.
Editorial
Loading solution…