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 == 40 <= arr[i] <= 9When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given four single-digit tiles and need to arrange them to form the latest possible valid time of day on a 24-hour clock. The brute force approach relies on trying every single possible arrangement of the four digits to see which ones make a legal time and then picking the biggest one.
Here's how the algorithm would work step-by-step:
import itertools
def largest_time_from_digits(digit_list):
# Generate all permutations to evaluate every possible sequence of the four input digits
all_permutations = itertools.permutations(digit_list)
latest_time_found = -1
for current_permutation in all_permutations:
hour_tens, hour_ones, minute_tens, minute_ones = current_permutation
hour_value = hour_tens * 10 + hour_ones
minute_value = minute_tens * 10 + minute_ones
# Validate the constructed time against 24-hour clock rules to filter out illegal times
if hour_value < 24 and minute_value < 60:
total_time_minutes = hour_value * 60 + minute_value
# Track the maximum valid time encountered so far to ensure we return the latest option
if total_time_minutes > latest_time_found:
latest_time_found = total_time_minutes
# Handle cases where no valid time can be formed by returning an empty string
if latest_time_found == -1:
return ""
final_hour_value = latest_time_found // 60
final_minute_value = latest_time_found % 60
return f"{final_hour_value:02d}:{final_minute_value:02d}"Since there are only a very small number of ways to arrange four digits, the most reliable strategy is to test every single possible combination of the given digits to see which ones form a valid time. Among all the valid times you can build, you simply select the one that represents the latest time of day.
Here's how the algorithm would work step-by-step:
import itertools
def largest_time_from_digits(digit_list):
latest_time_found = -1
# Evaluating every permutation ensures we do not miss any valid time combination.
for current_permutation in itertools.permutations(digit_list):
hour_value = current_permutation[0] * 10 + current_permutation[1]
minute_value = current_permutation[2] * 10 + current_permutation[3]
# Restricting hours and minutes to standard limits filters out impossible clock times.
if hour_value < 24 and minute_value < 60:
total_time_in_minutes = hour_value * 60 + minute_value
latest_time_found = max(latest_time_found, total_time_in_minutes)
# Returning an empty string signifies that no valid time could be constructed.
if latest_time_found == -1:
return ""
final_hour_part = latest_time_found // 60
final_minute_part = latest_time_found % 60
return f"{final_hour_part:02d}:{final_minute_part:02d}"| Case | How to Handle |
|---|---|
| Input array has fewer than four elements | Return an empty string immediately as a valid HH:MM time requires exactly four digits. |
| Input array has more than four elements | Return an empty string or slice the input to strictly process the first four digits. |
| No valid 24-hour time can be formed from the given digits | Return an empty string after checking all possible permutations and finding none that satisfy hour under 24 and minute under 60. |
| All four digits are identical | Generate the single unique permutation and evaluate if it forms a valid time. |
| Input contains digits that make hours greater than 23 | Permutations resulting in hours from 24 to 99 are filtered out during the validation check. |
| Multiple valid times can be formed | Iterate through all permutations, compare their numerical values, and retain the maximum valid time string. |
| Input array contains zeros | Process them as standard digits while ensuring leading zeros in hours and minutes are correctly formatted. |
| Maximum possible input size is fixed at four elements | Since the input size is constant, the algorithm runs in O(1) time complexity by evaluating all 24 permutations. |