Maximum Vacation Days

题目
LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm problems. But all work and no play makes Jack a dull boy, you could take vacations in some particular cities and weeks. Your job is to schedule the traveling to maximize the number of vacation days you could take, but there are certain rules and restrictions you need to follow.

答案

dfs

    public int maxVacationDays(int[][] flights, int[][] days) {
        return recur(flights, days, 0, 0);
    }

    public int recur(int[][] flights, int[][] days, int curr_city, int curr_week) {
        if(curr_week == days.length) {
            return 0;
        }
        int max = 0;
        // How about fly to the other N - 1 cities
        for(int i = 0; i < flights[0].length; i++) {
            if(flights[curr_city][i] == 1 || curr_city == i){
                int ret = recur(flights, days, i, curr_week + 1) + days[i][curr_week];
                max = Math.max(max, ret);
            }
        }
        return max;
    }

dfs + memorization

class Solution {
    public int maxVacationDays(int[][] flights, int[][] days) {
        int[][] dp = new int[flights.length][days[0].length];
        for(int i = 0; i < dp.length; i++) {
            Arrays.fill(dp[i], -1);
        }
        return recur(dp, flights, days, 0, 0);
    }

    public int recur(int[][] dp, int[][] flights, int[][] days, int curr_city, int curr_week) {
        if(curr_week == days[0].length) {
            return 0;
        }
        int max = 0;
        if(dp[curr_city][curr_week] != -1) return dp[curr_city][curr_week];

        // How about fly to the other N - 1 cities
        for(int i = 0; i < flights[0].length; i++) {
            if(flights[curr_city][i] == 1 || curr_city == i){
                int ret = recur(dp, flights, days, i, curr_week + 1) + days[i][curr_week];
                max = Math.max(max, ret);
            }
        }
        dp[curr_city][curr_week] = max;
        return max;
    }
}

dp

class Solution {
    public int maxVacationDays(int[][] flights, int[][] days) {
        int N = flights.length, K = days[0].length;
        int[][] dp = new int[N][K + 1];
        // Base cases
        for(int i = 0; i < N; i++) {
            dp[i][K] = 0;
        }

        int max = 0;
        for(int i = K - 1; i >= 0; i--) {
            for(int j = 0; j < N; j++) {
                for(int k = 0; k < N; k++) {
                    if(flights[j][k] == 1 || j == k) {
                        dp[j][i] = Math.max(dp[j][i], dp[k][i + 1] + days[k][i]);
                    }

                }
                max = Math.max(max, dp[j][i]);
            }
        }
        return dp[0][0];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,931评论 0 10
  • LeetCode wants to give one of its best employees the opti...
    Jeanz阅读 881评论 0 0
  • 人生就像一只列车 笔直地向前开去 有些人在上车时邂逅 共度一段美好时光 到站了 就散了
    clyq阅读 281评论 0 0
  • 休息第三天,感觉好腐败。睡了三天。早知道就去玩了。 突然看到时间,不是为了任务,就是自己喜欢写一写,让自己不是那么...
    梦游世界阅读 347评论 0 0
  • 福建泉港随记
    细雨丽华阅读 203评论 0 0

友情链接更多精彩内容