Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Event is the container class for when a specific group of people are meeting and are therefore
* busy. Events are considered read-only.
*/
public final class Event {
public final class Event implements Comparable<Event>{
private final String title;
private final TimeRange when;
private final Set<String> attendees = new HashSet<>();
Expand Down Expand Up @@ -75,6 +75,9 @@ public Set<String> getAttendees() {
// internal data.
return Collections.unmodifiableSet(attendees);
}
public int compareTo(Event eventB) {
return this.getWhen().start() - eventB.getWhen().start();
}

@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,56 @@

package com.google.sps;

import java.util.Collection;
import java.util.*;
import java.lang.Math;

public final class FindMeetingQuery {
public Collection<TimeRange> query(Collection<Event> events, MeetingRequest request) {
throw new UnsupportedOperationException("TODO: Implement this method.");
long duration = request.getDuration();
ArrayList<Event> allEvents = new ArrayList();
Set<String> attendees = new HashSet<String>(request.getAttendees());
//Add all events that attendees go to
for (Event event : events) {
Set<String> curAttendees = new HashSet<String>(attendees);
curAttendees.retainAll(event.getAttendees());
if (!(curAttendees.isEmpty())){
allEvents.add(event);
}
}
Collections.sort(allEvents);

ArrayList<TimeRange> times = new ArrayList();
int start = 0;
Boolean cont = false;
for (int i = 0; i < allEvents.size(); i++) {
TimeRange curTime = allEvents.get(i).getWhen();
if (!cont) {
TimeRange curRange = TimeRange.fromStartEnd(start, curTime.start(), false);
if ((long) curRange.duration() >= request.getDuration()) {
times.add(curRange);
}
}
//If next event overlaps with current, make start the greater of the two events
if ((i != allEvents.size() - 1) && curTime.overlaps(allEvents.get(i+1).getWhen())) {
start = Math.max(curTime.end(), start);
cont = true;
} else {
//If it doesn't overlap check gap during next iteration
start = Math.max(curTime.end(), start);
cont = false;
}
//If last event check if end of day is a possible time
if (i == allEvents.size() - 1) {
TimeRange curRange = TimeRange.fromStartEnd(start, TimeRange.END_OF_DAY, true);
if ((long) curRange.duration() >= request.getDuration()) {
times.add(curRange);
}

}
}
if (allEvents.size() == 0 && request.getDuration() <= TimeRange.END_OF_DAY) {
times.add(TimeRange.fromStartEnd(TimeRange.START_OF_DAY, TimeRange.END_OF_DAY, true));
}
return times;
}
}