2021-11-08

class order:
    """
    @param score: When the j-th driver gets the i-th order, we can get score[i][j] points.
    @return: return an array that means the array[i]-th driver gets the i-th order.
    """
    def orderAllocation(self, score):
        n = len(score)
        if n == 0:
            return []
        if n == 1:
            return [0]
        visit = [0] * n
        res = [0] * n
        val = -1
        def dfs(i, path, total):
            if i == n:
                nonlocal val
                if total > val:
                    val = total
                    res[:] = path[:]
                return
            for j in range(n):
                if visit[j] == 1:
                    continue
                visit[j] = 1
                path.append(j)
                dfs(i + 1, path, total + score[i][j])
                path.pop()
                visit[j] = 0

        dfs(0, [], 0)
        return res
score=[[1,2,4],[7,11,16],[37,29,22]]
model=order()
print(model.orderAllocation(score))
上一篇:C++关于树的一些常用代码实现


下一篇:全排列二 python (leetcode)