SandTimers
SRM 245 · 2005-06-01 · by vorthys
Problem Statement
Sigh... If only the king could make up his mind. First he wanted rounds in the Royal Wrestling Tournament to last for 2 minutes, then he changed his mind to 3 minutes. He wanted periods in the Royal Hockey Tournament to last for 20 minutes, then he decided on 15. Your job as the Royal Time Keeper would be much easier if he would buy you a modern stopwatch, but he insists that you use the Royal Sand Timers, which have been in his family since the 14th century. You're worried that someday he is going to ask you to measure a time interval that simply can't be measured with the timers you have available.
Given a
For example, if you had a 5-minute timer and a 7-minute timer, you could easily measure intervals of 5, 7, and 10 minutes. With a little more trouble you can measure intervals of 2, 3, 4, and 9 minutes. To measure 4 minutes, you start by flipping over both timers. When the 5-minute timer runs out, you flip it over and yell "Start", leaving the 7-minute timer running. When the 7-minute timer runs out two minutes later, you again flip over the 5-minute timer, which has been running for two minutes. The 5-minute timer runs out two minutes later, and you yell "Stop". However, no matter how you try, you cannot find a way to measure 1, 6, or 8 minutes, assuming the king's patience is limited to 10 minutes.
You will return a
Constraints
- timers contains between 1 and 3 elements, inclusive.
- Each element of timers is between 1 and 20, inclusive.
- maxInterval is between 1 and 360, inclusive.
- maxTime is between 1 and 360, inclusive.
- maxInterval is no greater than maxTime.
{ 5, 7 }
10
10
Returns: {1, 6, 8 }
The example above.
{ 20,20,19 }
360
360
Returns: { }
{ 2, 10, 20 }
30
40
Returns: {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29 }
All your timers are even, so you can't measure an odd number of minutes.
{ 2, 5, 9 }
360
360
Returns: { }
You can measure all possible intervals.
{ 4 }
23
47
Returns: {1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, 21, 22, 23 }
{17,19}
1
153
Returns: { }
19*8=152 17*9=153
{3,5}
1
6
Returns: { }
0 | Turn over 3 and 5 3 | 3 runs out, turn over 5 | 5 runs out, START 6 | 3 runs out, STOP
Submissions are judged against all 72 archived test cases, of which 7 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SandTimers with a public method vector<int> badIntervals(vector<int> timers, int maxInterval, int maxTime) · 72 test cases · 2 s / 256 MB per case