zoukankan      html  css  js  c++  java
  • [leetcode]Maximum Vacation Days

    思路简单,状态转移方程容易想。需要注意的是转移时的一些状态,比如是否连通,等等。

    class Solution:
        def maxVacationDays(self, flights: List[List[int]], days: List[List[int]]) -> int:
            
            n = len(flights)
            k = len(days[0])
            
            adjLists = {} 
            
            for i in range(n):
                for j in range(n):
                    if flights[i][j] == 1 or i == j: # i --> j
                        if j not in adjLists:
                            adjLists[j] = []
                        adjLists[j].append(i)
                        
            
            memo = {} # i, j : i - city; j - week;  max days for vacation
            
            result = 0
            
            for j in range(k):
                for i in range(n):
                    if j == 0: # first week
                        if 0 in adjLists[i]:
                            memo[(i, j)] = days[i][j]
                        else:
                            memo[(i, j)] = -1
                    else: # j > 0
                        maxTmp = -1
                        for city in adjLists[i]:
                            maxTmp = max(memo[(city, j - 1)], maxTmp)
                        if maxTmp != -1:
                            memo[(i, j)] = maxTmp + days[i][j]
                        else:
                            memo[(i, j)] = -1
                        
                    result = max(result, memo[(i, j)])
            
            return result
    

      

  • 相关阅读:
    软件测试的术语及解释总结
    测试工具
    数据库客户端连接
    应用程序发生错误怎么修改
    软件测试容易忽略的缺陷
    LR中错误解决方法
    LR结果分析2
    LR结果分析
    Hive
    zookeeper
  • 原文地址:https://www.cnblogs.com/lautsie/p/12257986.html
Copyright © 2011-2022 走看看