TopCoder problem "Cafeteria" used in SRM 229 (Division I Level One , Division II Level Two)

Problem Statement

    During the week, the cafeteria of my university is open until 2:30 PM. Normally, I will be at the university before this time anyway. Since I am a lazy person, I don't want to cook for myself and prefer eating in the cafeteria. But sometimes I don't have any classes in the morning, so I have to make sure to get to the university before the cafeteria closes.

There are several bus stops within walking distance of my home and each one has a bus connection to the university. For each bus stop, there will be a bus driving to the university every ten minutes, so I just need to remember the number of minutes after a full hour when the first bus departs from each stop, how long it takes to get to the bus stop and how long the bus needs to drive to the university.

Given a int[] offset (the number of minutes after a full hour when the first bus departs), int[] walkingTime (the number of minutes I need to walk to a bus stop) and a int[] drivingTime (the number of minutes the bus needs to drive to the university) you should write a method that returns the latest time when I have to leave my house in order to be at the university at 2:30 PM or before. The ith element of offset, walkingTime and drivingTime refers to the ith bus stop.

The format of the returned time should be HH:MM (HH the hour, MM the minute). The 12 hour time format is used (see examples).


Parameters:int[], int[], int[]
Method signature:String latestTime(int[] offset, int[] walkingTime, int[] drivingTime)
(be sure your method is public)


-Times between noon and 1 PM should be written as 12:MM (where MM is the number of minutes after noon).


-offset, walkingTime and drivingTime contain the same number of elements.
-offset, walkingTime and drivingTime contain between 1 and 50 elements, inclusive.
-Each element of offset is between 0 and 9, inclusive.
-Each element of walkingTime is between 1 and 30, inclusive.
-Each element of drivingTime is between 1 and 300, inclusive.


Returns: "02:28"
There is only one bus stop. One bus departs at 2:29 and arrives at 2:30. Since the time to walk to the bus stop is 1 minute, the return value is 02:28.
Returns: "12:17"
Leaving the house at 12:17 lets me catch the bus at 12:26 which arrives at 2:26 at the university. Note that the bus departing at 12:36 would arrive too late.
Returns: "12:19"
Although the bus needs one minute more from the 2nd bus stop, and the walking time is also one minute longer, it is still better to leave the house at 12:19 and catch the bus at the 2nd bus stop at 12:29.
Returns: "11:09"
This simulates a bus departing every minute.
Returns: "02:07"

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , brett1479

Problem categories:

Brute Force, Simulation