Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.ArrayList; import java.util.Comparator; class Train { int trainId; String arrivalTime; String departureTime; public Train ( int trainId, String arrivalTime, String departureTime ) { this.trainId
import java.util.ArrayList;
import java.util.Comparator;
class Train
int trainId;
String arrivalTime;
String departureTime;
public Trainint trainId, String arrivalTime, String departureTime
this.trainId trainId;
this.arrivalTime arrivalTime;
this.departureTime departureTime;
class TrainScheduleManager
ArrayList trains;
public TrainScheduleManager
this.trains new ArrayList;
public void addTrainint trainId, String arrivalTime, String departureTime
Train newTrain new TraintrainId arrivalTime, departureTime;
int index binarySearchInsertPositionarrivalTime;
trains.addindex newTrain;
private int binarySearchInsertPositionString targetArrivalTime
int low ;
int high trains.size;
while low high
int mid low high low;
String midArrivalTime trains.getmidarrivalTime;
if midArrivalTimecompareTotargetArrivalTime
low mid ;
else if midArrivalTimecompareTotargetArrivalTime
high mid ;
else
Handle cases where arrival times are the same unlikely for this problem
low mid ; Insert after duplicates if any
return low; Insert position
public void removeTrainint trainId
for int i ; i trains.size; i
if trainsgetitrainId trainId
trains.removei;
break;
public void displaySchedule
for Train train : trains
System.out.printlnTrain ID: train.trainId
Arrival: train.arrivalTime
Departure: train.departureTime;
public class Main
public static void mainString args
TrainScheduleManager manager new TrainScheduleManager;
manager.addTrain::;
manager.addTrain::;
manager.addTrain::;
manager.addTrain::;
System.out.printlnInitial Train Schedule:";
manager.displaySchedule;
manager.removeTrain;
System.out.printlnUpdated Train Schedule after removing Train :;
manager.displaySchedule;
what is the time complexity and space complexity of this code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started