4个数字凑最大时间

Give four digits, find the maximum valid time that can be displayed on a digital clock(in 24 hours format) using those digits. For example, given digits 1, 8, 3, 2, the maximum valid time is “23:18”.

<pre name="code" class="python">import functools


test_arr = [
    [1, 8, 3, 2],
    [2, 4, 0, 0],
    [3, 0, 7, 0],
    [9, 1, 9, 7],
    [2, 6, 0, 6]]


def findAllRange(arr, pool, length):
    if length >= 4:
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span>pool.append(arr[:])</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span>return</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">	</span>for i in range(0, length + 1):</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span>arr[i], arr[length] = arr[length], arr[i]</span>
       findAllRange(arr, pool, length + 1)
       arr[length], arr[i] = arr[i], arr[length]


def isTime(pool):
    res = []
    for i in pool:
        if not (((i[0] * 10 + i[1]) > 23) or ((i[2] * 10 + i[3]) > 59)):
            res.append(i)
    return res


def comp(a, b):
    for i in range(0, 4):
        if a[i] < b[i]:
            return 1
        elif a[i] == b[i]:
           continue
        else:
            return -1
    return 0


def solution(A, B, C, D):
    arr = [A, B, C, D]
    pool = []
    findAllRange(arr, pool, 0)
    pool = sorted(pool, key = functools.cmp_to_key(comp))
    pool = isTime(pool)
    if len(pool) <= 0:
        return 'NOT POSSIBLE'
    else:
        return '%d%d:%d%d' % (pool[0][0], pool[0][1], pool[0][2], pool[0][3])


if __name__ == '__main__':
    for i in test_arr:
        print(solution(i[0], i[1], i[2], i[3]))

点赞