Given an array A
of four digits, return the largest 24-hour time that can be made. The time should be returned as a string in the format "HH:MM"
. If no valid time can be made, return an empty string.
A
must be used exactly once.00:00
to 23:59
.""
(empty string).
Example:
Input: A = [1,2,3,4]
Output: "23:41"
To solve this problem, we need to arrange the four digits into a valid time in HH:MM
format, making sure that the hour is between 00
and 23
, and the minute is between 00
and 59
. We must use each digit exactly once.
The brute-force way is to try every possible ordering (permutation) of the four digits and check if it forms a valid time. Since there are only 24 permutations (4!), this is feasible. For each permutation, we can check if the first two digits form a valid hour and the last two a valid minute.
After checking all possibilities, we should keep track of the largest valid time found. The problem is not about efficiency but about correctness and careful validation.
There is little room for optimization since the input size is so small, but we should make sure our validation logic is correct and that we always return the largest time.
Let's break down the approach step-by-step:
HH
), and the last two as the minute (MM
).0 <= HH < 24
and 0 <= MM < 60
.HH
and MM
to minutes since midnight for easy comparison)."HH:MM"
with leading zeros if necessary.This approach is simple and effective due to the small input size.
Let's walk through the example A = [1,2,3,4]
:
"23:41"
.Brute-force Approach:
The problem asks us to find the largest valid 24-hour time from four digits, using each digit exactly once. The key insight is that the number of possible arrangements is so small that we can simply check every permutation. For each, we validate the time and track the largest. This brute-force approach is both simple and effective, and ensures correctness by exhaustively checking all options.
from itertools import permutations
class Solution:
def largestTimeFromDigits(self, A):
max_time = -1
for perm in permutations(A):
h1, h2, m1, m2 = perm
hour = h1 * 10 + h2
minute = m1 * 10 + m2
if 0 <= hour < 24 and 0 <= minute < 60:
total = hour * 60 + minute
if total > max_time:
max_time = total
if max_time == -1:
return ""
else:
hour = max_time // 60
minute = max_time % 60
return "{:02d}:{:02d}".format(hour, minute)
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
string largestTimeFromDigits(vector<int>& A) {
string res = "";
int max_time = -1;
sort(A.begin(), A.end());
do {
int hour = A[0] * 10 + A[1];
int minute = A[2] * 10 + A[3];
if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60) {
int total = hour * 60 + minute;
if (total > max_time) {
max_time = total;
char buffer[6];
sprintf(buffer, "%02d:%02d", hour, minute);
res = buffer;
}
}
} while (next_permutation(A.begin(), A.end()));
return res;
}
};
import java.util.*;
class Solution {
public String largestTimeFromDigits(int[] A) {
int max = -1;
String res = "";
List<Integer> list = new ArrayList<>();
for (int a : A) list.add(a);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j == i) continue;
for (int k = 0; k < 4; k++) {
if (k == i || k == j) continue;
int l = 6 - i - j - k;
int hour = list.get(i) * 10 + list.get(j);
int minute = list.get(k) * 10 + list.get(l);
if (hour < 24 && minute < 60) {
int total = hour * 60 + minute;
if (total > max) {
max = total;
res = String.format("%02d:%02d", hour, minute);
}
}
}
}
}
return res;
}
}
function largestTimeFromDigits(A) {
let max = -1, res = "";
function permute(arr, l) {
if (l === arr.length) {
let hour = arr[0] * 10 + arr[1];
let minute = arr[2] * 10 + arr[3];
if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60) {
let total = hour * 60 + minute;
if (total > max) {
max = total;
res = ("0" + hour).slice(-2) + ":" + ("0" + minute).slice(-2);
}
}
return;
}
for (let i = l; i < arr.length; i++) {
[arr[l], arr[i]] = [arr[i], arr[l]];
permute(arr, l + 1);
[arr[l], arr[i]] = [arr[i], arr[l]];
}
}
permute(A, 0);
return res;
}