I've written an answer to HackerRank's Angry Professor problem. Please let me know if there's a quicker/better way of solving this as I think I wrote quite a bit of code for this problem (answer has to be in 1 class btw). Also, any improvements on my code in Java 8 are welcome too.
Problem statement (as written on HackerRank):
The professor is conducting a course on Discrete Mathematics to a class of N students. He is angry at the lack of their discipline, and he decides to cancel the class if there are less than K students present after the class starts.
Given the arrival time of each student, your task is to find out if the class gets cancelled or not.
Answer:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AngryProfessor {
public static void main(String[] args) {
List<List<List<Integer>>> testCases = readInput();
for(List<List<Integer>> testCase : testCases){
int numStudents = testCase.get(0).get(0);
int minStudents = testCase.get(0).get(1);
// Gotta STDOUT in hacker rank for it to be graded.
System.out.println(isLectureCancelled(calcNumAttending(testCase.get(1), numStudents), minStudents));
}
}
private static void addData(List<Integer> arrayList, String[] numbersToAdd){
for(String num : numbersToAdd){
arrayList.add(Integer.valueOf(num));
}
}
/**
* Calculates the number of students that make the lecture on time.
*
* @param studentTimes the relative time at which the student arrives (student is on time if time is <= 0)
* @param numStudents number of students that make the lecture on time
* @return numStudents
*/
private static int calcNumAttending(List<Integer> studentTimes, int numStudents){
int numAttending = 0;
for(int i = 0; i < numStudents; i++){
if(studentTimes.get(i) <= 0){
numAttending++;
}
}
return numAttending;
}
/**
* Determines whether the lecture is cancelled or not.
*
* @param numAttending Number of students who attend the lecture
* @param minStudents Minimum number of students for the lecture to go ahead
* @return whether the lecture is cancelled or not
*/
private static String isLectureCancelled(int numAttending, int minStudents){
return numAttending < minStudents ? "YES" : "NO";
}
/**
* Reads and stores all the data given. Each index is a test case. At that index is a list containing two lists. One
* for each line of input that's read. The first list contains numStudents and minStudents, and the second list
* contains the student times.
*
* @return a List containing all the data.
*/
private static List<List<List<Integer>>> readInput(){
Scanner k = new Scanner(System.in);
int numTestCases = Integer.valueOf(k.nextLine());
List<List<List<Integer>>> testCases = new ArrayList<>(numTestCases);
// fills up testCases with ArrayLists before filling up with numbers
for(int i = 0; i < numTestCases; i++){
List<List<Integer>> list = new ArrayList<>(2);
for(int j = 0; j < 2; j++){
list.add(new ArrayList<>());
}
testCases.add(list);
}
// Inputs data into the ArrayLists
for(int i = 0; i < numTestCases; i++) {
String line1 = k.nextLine();
String line2 = k.nextLine();
addData(testCases.get(i).get(0), line1.split(" "));
addData(testCases.get(i).get(1), line2.split(" "));
}
k.close();
return testCases;
}
}