TopCoder problem "BrokenClock" used in SRM 290 (Division I Level One , Division II Level Two)



Problem Statement

    

You have noticed that your digital clock does not show the correct current time. You know what the correct time is, so there should be no problem setting it. Your clock has two buttons - one is supposed to increment the hour by 1, and the other is supposed to increment the minute by 1. The hour is a two digit value between 00 and 23, inclusive, and the minute is a two digit value between 00 and 59, inclusive. The values wrap around, so pressing the hour button when the hour is 23 will change the hour to 00, and pressing the minute button when the minute is 59 will change the minute to 00. Pressing the hour button should never affect the minute value, and pressing the minute button should never affect the hour value (see note). However, the hour button on your clock is broken, and when pressed, increments both the hour and minute by 1. You want to find the minimal number of button presses needed to set the correct time on the clock.

For example, if the clock shows the time as 03:12 and you know the current time is 04:15, you should press the hour button once and the minute button twice for a total of three button presses. After pressing the hour button, the time changes to 04:13, and after pressing the minute button twice, the time changes to 04:15. You must perform the button presses immediately - you cannot wait until the time on the clock changes by itself.

You are given a String clockTime, the time shown on the clock, and a String currentTime, the correct current time, both formatted as "HH:MM" (quotes added for clarity), where HH is the hour value and MM is the minute value.

 

Definition

    
Class:BrokenClock
Method:fewestClicks
Parameters:String, String
Returns:int
Method signature:int fewestClicks(String clockTime, String currentTime)
(be sure your method is public)
    
 

Notes

-If the clock displays 23:59, pressing the hour button will change the time to 00:00, but pressing the minute button will change it to 23:00.
 

Constraints

-clockTime and currentTime are each formatted as "HH:MM" (quotes added for clarity) where HH is a two digit integer between 00 and 23, inclusive, and MM is a two digit integer between 00 and 59, inclusive.
 

Examples

0)
    
"03:12"
"04:15"
Returns: 3
The example from the problem statement.
1)
    
"07:07"
"13:21"
Returns: 14
You have to press the hour button six times and the minute button eight times.
2)
    
"14:55"
"14:05"
Returns: 10
The minute button never changes the hour.
3)
    
"23:14"
"00:20"
Returns: 6
4)
    
"18:43"
"18:43"
Returns: 0

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=5962

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=9811&pm=5962

Writer:

hauser

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Math, Simple Search, Iteration