-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlargestTimeFromDigits.go
More file actions
50 lines (39 loc) · 956 Bytes
/
Copy pathlargestTimeFromDigits.go
File metadata and controls
50 lines (39 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* https://leetcode.com/problems/largest-time-for-given-digits/description/
Given an array of 4 digits, return the largest 24 hour time that can be made.
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
Example 1:
Input: [1,2,3,4]
Output: "23:41"
Example 2:
Input: [5,5,5,5]
Output: ""
Note:
A.length == 4
0 <= A[i] <= 9
*/
package lmath
import (
"fmt"
"sort"
)
func largestTimeFromDigits(A []int) string {
sort.Ints(A)
for i := 3; i >= 0; i-- {
if A[i] > 2 {
continue
}
for j := 3; j >= 0; j-- {
if j == i || (A[i] == 2 && A[j] > 3) {
continue
}
for m := 3; m >= 0; m-- {
if m == i || m == j || A[m] > 5 {
continue
}
return fmt.Sprintf("%d%d:%d%d", A[i], A[j], A[m], A[6-i-j-m])
}
}
}
return ""
}