Given an array arr
of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.
24-hour times are formatted as "HH:MM"
, where HH
is between 00
and 23
, and MM
is between 00
and 59
. The earliest 24-hour time is 00:00
, and the latest is 23:59
.
Return the latest 24-hour time in "HH:MM"
format. If no valid time can be made, return an empty string.
Example 1:
Input: arr = [1,2,3,4] Output: "23:41" Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.
Example 2:
Input: arr = [5,5,5,5] Output: "" Explanation: There are no valid 24-hour times as "55:55" is not valid.
Constraints:
arr.length == 4
0 <= arr[i] <= 9
The problem asks to find the largest possible 24-hour time that can be formed using four given digits, each exactly once. The time should be in "HH:MM" format, where HH represents hours (00-23) and MM represents minutes (00-59).
Both solutions presented utilize a brute-force approach by iterating through all possible combinations of the four digits to form hours and minutes. They differ slightly in implementation style.
This solution first counts the frequency of each digit using a counter array cnt
. Then it iterates through possible hours (h
) from 23 down to 0 and minutes (m
) from 59 down to 0. For each hour-minute pair, it creates a temporary frequency count t
to check if it matches the original digit frequency cnt
. If a match is found, the corresponding time is formed and returned. If no valid time is found, an empty string is returned.
Time Complexity Analysis: The outer loop iterates at most 24 times (for hours), and the inner loop iterates at most 60 times (for minutes). The comparison of frequency counts (cnt == t
) takes O(10) time in the worst case (because we have 10 possible digits). The overall time complexity is O(24 * 60 * 10) which simplifies to O(1). This is because the input size is fixed at 4 digits.
Space Complexity Analysis: O(1) because we use a fixed-size array cnt
of size 10 to store digit frequencies.
This solution directly generates all possible permutations of the four digits to form hours and minutes. Three nested loops iterate through all combinations of choosing the digits for the tens place of the hour, the units place of the hour, and the tens place of the minutes. The remaining digit automatically fills the units place of the minutes. For each combination, it checks if the formed hour and minute are valid (within the 24-hour clock constraint). If valid, it updates the ans
variable to store the maximum time found. Finally, it formats and returns the largest time found or an empty string if no valid time exists.
Time Complexity Analysis: The three nested loops iterate through 4 * 4 * 4 = 64 combinations. The time complexity is O(1), as it's a fixed number of iterations regardless of input size.
Space Complexity Analysis: O(1) because it uses a constant amount of extra space to store variables like ans
.
Which solution is better?
Although both have O(1) time complexity because the input size is fixed, Solution 1 is slightly more efficient because it avoids unnecessary calculations by first checking the digit frequencies. Solution 2 generates all possible combinations even if the digits don't allow a valid time. However, the difference in runtime will be negligible in practice for this specific problem. Solution 1 is arguably more readable and easier to understand due to the more structured approach of using frequency counting.