# September LeetCoding Challenge, Day 1: Largest Time for Given Digits

September 2, 2020

LeetCode is a platform with programming challenges. They’re designed around helping prepare for technical interviews, but they also host programming contests and have some explore packages, which are bundles of problems and articles. In April 2020, they started providing monthly explore packages in the form of challenges, in which they’d add a problem each day of the month. Solving all problems within 24 hours of their release makes you eligible for some rewards. I have been solving these monthly challenges for fun since April, but decided to start writing a bit about them in September. It’s very easy to find solutions for most of these problems online, but I’ll try to post these the day after the challenge, to avoid unnecessary copying. I’m solving these problems in C++, since it’s my language of choice for this type of algorithmic programming problems.

The problem for September 1 is Largest Time for Given Digits. The statement is straightforward: given 4 digits, return the largest 24 hour time (in a “HH:MM” string) that can be made using all of those digits, or an empty string if no valid time can be made.

A possible solution for this problem is to enumerate all valid times and check the largest one that uses all of the provided digits. Since there are at most $$24 \times 60 = 1440$$ valid times, this is good enough to avoid a time limit exceeded. The following is an implementation of that strategy:

Another possible solution is to iterate through all the permutations of the provided digits, and return the one that produces a valid time and is the largest. Since there are only $$4! = 24$$ permutations, this runs much faster than the previous solution. The following is an implementation of that strategy:

After reading this problem for the first time, I was under the impression that a greedy algorithm that would pick the next largest available digit satisfying the following restrictions would also work:

• The digit in the tens place of the hours number must not be larger than 2;
• The digit in the ones place of the hours number must not be larger than 3 if the digit in the tens place of the hours number is equal to 2;
• The digit in the tens place of the minutes number must not be larger than 5.

However, it’s easy to come up with a counter example in which this algorithm would fail. If provided with the digits $$[0, 2, 6, 6]$$, the correct output would be “06:26”. However, the previously described algorithm would fail to produce a valid answer, since it’d try to use digits $$[0, 2]$$ for the hours and be left with $$[6, 6]$$ for the minutes, which can’t produce a valid minute number.