- 145.6k
- 22
- 190
- 479
0,9,5,5,5,5,5,5,5,9
→Impossible
(should be09:55:55
)
2,0,2,5,5,5,5,5,5,5
→Impossible
(should be20:55:55
)Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,2,4,4,4,4,4,4,7
→24:47:44
(should be22:47:44
)The challenge states that
24:00:00
is the upper bound. The correct answer should be,22:47:44
. Note that this means, demonstrates that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select24
for the hour (which would work for input such as0,0,0,0,2,4,9,9,9
). However, for the input2,2,4,4,4,4,4,4,7
, you should then find that there is no valid minute if you pick24
as the hour, and you would need to backtrack and try22
for the hour.
0,9,5,5,5,5,5,5,5
→Impossible
2,0,5,5,5,5,5,5,5
→Impossible
Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,2,4,4,4,4,4,4,7
→24:47:44
The challenge states that
24:00:00
is the upper bound. The correct answer should be22:47:44
. Note that this means that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select24
for the hour (which would work for input such as0,0,0,0,2,4,9,9,9
). However, for the input2,2,4,4,4,4,4,4,7
, you should then find that there is no valid minute if you pick24
as the hour, and you would need to backtrack and try22
for the hour.
0,5,5,5,5,5,5,5,9
→Impossible
(should be09:55:55
)
0,2,5,5,5,5,5,5,5
→Impossible
(should be20:55:55
)Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,2,4,4,4,4,4,4,7
→24:47:44
(should be22:47:44
)The challenge states that
24:00:00
is the upper bound. The correct answer,22:47:44
, demonstrates that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select24
for the hour (which would work for input such as0,0,0,0,2,4,9,9,9
). However, for the input2,2,4,4,4,4,4,4,7
, you should then find that there is no valid minute if you pick24
as the hour, and you would need to backtrack and try22
for the hour.
0,9,5,5,5,5,5,5,5
→Impossible
2,0,5,5,5,5,5,5,5
→Impossible
Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,42,54,54,54,54,54,54,57
→24:5547:5544
The challenge states that
24:00:00
is the upper bound. The correct answer should be22:47:44
. Note that this means that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select24
for the hour (which would work for input such as0,0,0,0,2,4,9,9,9
). However, for the input2,2,4,4,4,4,4,4,7
, you should then find that there is no valid minute if you pick24
as the hour, and you would need to backtrack and try22
for the hour.
Educational tips
The solution above uses some rather advanced techniques, and may be overwhelming for a beginner. However, I would point out an essential next step for improving your coding skills.
It is critical to learn to write functions, each with a clearly defined purpose, that accept parameters, return results, and use no global variables. Document the purpose, parameters, and outputs in a docstring, as I've done. That will force you to package your code into small, reusable chunks. For example, can you write a function that accepts a string (or list) of characters, and that returns a tuple with two items — the best possible hour that can be formed from those characters, and all the leftover characters that were not used to form the hour?
Defining a function is necessary to solve this problem correctly, because, as I've pointed out above, a correct solution requires some trial and error. If you are unable to reuse the code that performs the trials, then you will end up writing if-else statements to handle all of the failure cases, and it's going to be a huge mess!
0,9,5,5,5,5,5,5,5
→Impossible
2,0,5,5,5,5,5,5,5
→Impossible
Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,4,5,5,5,5,5,5,5
→24:55:55
The challenge states that
24:00:00
is the upper bound.
0,9,5,5,5,5,5,5,5
→Impossible
2,0,5,5,5,5,5,5,5
→Impossible
Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,2,4,4,4,4,4,4,7
→24:47:44
The challenge states that
24:00:00
is the upper bound. The correct answer should be22:47:44
. Note that this means that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select24
for the hour (which would work for input such as0,0,0,0,2,4,9,9,9
). However, for the input2,2,4,4,4,4,4,4,7
, you should then find that there is no valid minute if you pick24
as the hour, and you would need to backtrack and try22
for the hour.
Educational tips
The solution above uses some rather advanced techniques, and may be overwhelming for a beginner. However, I would point out an essential next step for improving your coding skills.
It is critical to learn to write functions, each with a clearly defined purpose, that accept parameters, return results, and use no global variables. Document the purpose, parameters, and outputs in a docstring, as I've done. That will force you to package your code into small, reusable chunks. For example, can you write a function that accepts a string (or list) of characters, and that returns a tuple with two items — the best possible hour that can be formed from those characters, and all the leftover characters that were not used to form the hour?
Defining a function is necessary to solve this problem correctly, because, as I've pointed out above, a correct solution requires some trial and error. If you are unable to reuse the code that performs the trials, then you will end up writing if-else statements to handle all of the failure cases, and it's going to be a huge mess!
0,9,5,5,5,5,5,5,5
→Impossible
2,0,5,5,5,5,5,5,5
→Impossible
Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,4,5,5,5,5,5,5,5
→24:55:55
The challenge states that
24:00:00
is the upper bound.
2,0,5,5,5,5,5,5,5
→Impossible
Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,4,5,5,5,5,5,5,5
→24:55:55
The challenge states that
24:00:00
is the upper bound.
0,9,5,5,5,5,5,5,5
→Impossible
2,0,5,5,5,5,5,5,5
→Impossible
Due to tests like
if hrs[0]
,if hrs[1]
,if mins[1]
, andif secs[1]
, hours, minutes, or seconds is a multiple of 10, or if the hour starts with0
.2,4,5,5,5,5,5,5,5
→24:55:55
The challenge states that
24:00:00
is the upper bound.