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.
Question
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.
Solution
This problem can be solved by using a greedy algorithm. Here are the steps:
-
Sort the array in descending order. This will allow us to start from the largest possible digit.
-
Check the first digit. If it's less than or equal to 2, keep it as the first digit of the hour. If not, look for the next digit that is less than or equal to 2. If there's no such digit, return an empty string because it's impossible to form a valid time.
-
Check the second digit. If the first digit of the hour is 2, the second digit should be less than or equal to 3. If the first digit is not 2, the second digit can be any remaining digit. If there's no valid digit, return an empty string.
-
Check the third digit. It should be less than or equal to 5 because it's the first digit of the minute. If there's no valid digit, return an empty string.
-
The fourth digit is the last remaining digit.
-
Combine the digits to form a time in "HH:MM" format.
Here is a Python code that implements the above steps:
def latestTime(arr):
arr.sort(reverse=True)
for i in range(4):
if arr[i] <= 2:
hour1 = arr.pop(i)
break
else:
return ""
for i in range(3):
if (hour1 == 2 and arr[i] <= 3) or (hour1 < 2):
hour2 = arr.pop(i)
break
else:
return ""
for i in range(2):
if arr[i] <= 5:
minute1 = arr.pop(i)
break
else:
return ""
minute2 = arr[0]
return str(hour1) + str(hour2) + ":" + str(minute1) + str(minute2)
This function takes an array of 4 digits as input and returns the latest 24-hour time that can be made using each digit exactly once. If no valid time can be made, it returns an empty string.
Similar Questions
949. Largest Time for Given DigitsMedium6961048Add to ListShareGiven 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] <= 9Accepted85,586Submissions243,556Seen this question in a real interview before?YesNoCompaniesRelated TopicsSimilar Questions
A clock is set right at 5 a.m. The clock loses 16 minutes in 24 hours.What will be the exact time when the clock indicates 10 p.m. on 4th day?9 p.m.0.510:45 p.m.11 p.m.
Given an integer array hours representing times in hours, return an integer denoting the number of pairs i, j where i < j and hours[i] + hours[j] forms a complete day.A complete day is defined as a time duration that is an exact multiple of 24 hours.For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on. Example 1:Input: hours = [12,12,30,24,24]Output: 2Explanation:The pairs of indices that form a complete day are (0, 1) and (3, 4).Example 2:Input: hours = [72,48,24,3]Output: 3Explanation:The pairs of indices that form a complete day are (0, 1), (0, 2), and (1, 2). Constraints:1 <= hours.length <= 1001 <= hours[i] <= 109
The time shown is in the 24-hour clock.Express the time in the 12-hour clock.23 42 → . p.m.
A clock is set right at 8:00 am. The clock gains 10 min in 24 hrs. What will be the true time when the clock indicates 1 pm on the following day?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.