TopCoder problem "TimeOfPossession" used in TCCC05 Qual 5 (Division I Level One)

Problem Statement

    In many sports, the time of possession is a much talked about statistic. This indicates the amount of time during a game that a particular team possesses the ball. In this problem, you will calculate the time of possession given the relevant events of the game. You will be given a String[], each element of which is formatted as "EVENT TIME", where EVENT is either "A", "B", "SWITCH", or "END". An "A" indicates that team A gains possession of the ball, while a "B" indicates that team B gains possession of the ball. "SWITCH" indicates that the ball changes possession from one team to the other (there are only two teams). "END" indicates the end of the game. Each TIME will be formatted as "minutes:seconds" where seconds contains exactly two digits, and minutes contains no extra leading zeros. Your task is to return a String formatted as "minutes:seconds" in the same format as the input, indicating the total time of possession for team A. The input will be sorted in non-descending order by time, and the first element will be either "A 0:00" or "B 0:00", while the last element, and only the last element, will have EVENT = "END".


Method signature:String getTime(String[] times)
(be sure your method is public)


-If multiple events occur at the same time, assume that the one that appears earlier in the input actually occurs earlier (see example 2).


-times will contain between 2 and 50 elements, inclusive.
-Each element of times will be formatted as "EVENT TIME"
-Each EVENT will be "A", "B", "END", or "SWITCH".
-The first EVENT will be "A" or "B", and the first TIME will be "0:00".
-The last EVENT will be "END".
-Each TIME will be between "0:00" and "999:59", inclusive, and the minutes will not have any extra leading zeros, while the seconds will contain exactly 2 digits.
-The TIMEs will be sorted in non-descending order.


{"A 0:00","SWITCH 1:23","END 3:00"}
Returns: "1:23"
A possesses the ball for the first 1:23, but never gets it again.
{"A 0:00","SWITCH 1:23","SWITCH 4:50","END 15:00"}
Returns: "11:33"
{"A 0:00","SWITCH 0:00","END 15:00"}
Returns: "0:00"

Problem url:

Problem stats url:




PabloGilberto , vorthys

Problem categories:

Simple Math, String Parsing