How to solve this hackerrank problem? (max events given start and end times)

hmmm, that gives me an idea, what if in my current code, instead of going from arrival time to departure time trying to find a free slot, I do it backwards like this....

Too bad I can't access the HackerRank anymore, but this seems to work on my online compiler with my test case. Thoughts?

public static int countMeetings(List<Integer> arrival, List<Integer> departure) {
    if (arrival.isEmpty() || departure.isEmpty()){
        return 0;
    }

    int maxMeetings = 0;
    Set<Integer> booked = new HashSet<>();

    // books time slots according to first come first serve
    // need to optimize time slots for maximum meetings
    for (int i = 0; i < arrival.size(); i++){
        for (int j = departure.get(i); j >= arrival.get(i); j--){
            if (!booked.contains(j)){
                maxMeetings++;
                booked.add(j);
                break;
            }
        }
    }

    return maxMeetings;
}
/r/learnprogramming Thread Parent