# 949. Largest Time for Given Digits

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/largest-time-for-given-digits/

> 内容描述

```


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
```

## 解题方案

> 思路 1
******- 时间复杂度: O(1)******- 空间复杂度: O(1)******

全排列求个最大值就行

```python
class Solution:
    def largestTimeFromDigits(self, A):
        """
        :type A: List[int]
        :rtype: str
        """
        def permute(num):
            if len(num) == 0: return []
            if len(num) == 1: return [num]
            res = []
            for i in range(len(num)):
                x  = num[i]
                xs = num[:i] + num[i+1:]             
                for j in permute(xs):
                    res.append([x] + j)
            return res
        permu = permute(A)
        cur_max = -1
        res = []
        for per in permu:
            if 0 <= per[0] * 10 + per[1] <= 23:
                if 0 <= per[2] * 10 + per[3] <= 59:
                    if (per[0] * 10 + per[1]) * 60 + per[2] * 10 + per[3] > cur_max:
                        cur_max = (per[0] * 10 + per[1]) * 60 + per[2] * 10 + per[3]
                        res = per
        if cur_max == -1:
            return ''
        return ''.join(str(i) for i in res)[:2] + ':' + ''.join(str(i) for i in res)[2:]
```
