Solution
Approach 1: Brute Force
Intuition
Try all possible times, and remember the largest one.
Algorithm (Java)
Iterate over all permutations (i, j, k, l)
of (0, 1, 2, 3)
. For each permutation, we can try the time A[i]A[j] : A[k]A[l]
.
This is a valid time if and only if the number of hours 10*A[i] + A[j]
is less than 24
; and the number of minutes 10*A[k] + A[l]
is less than 60
.
We will output the largest valid time.
Algorithm (Python)
For each possible ordering of the 4 digits, if it's a legal time and the time is greater than the one we have stored, update the answer.
Complexity Analysis

Time Complexity: .

Space Complexity: .
Analysis written by: @awice. Java implementation inspired by @rock.