C++ DP solution, O(n) space, O(kn^2) time


  • 0
    A
    class Solution {
    public:
        int maxVacationDays(vector<vector<int>>& flights, vector<vector<int>>& days) {
            int n = flights.size(), k = days[0].size();
            vector<int> dp(n, INT_MIN);
            dp[0] = 0;
            for (int i = 0; i < k; ++i) {
                vector<int> prev = dp;
                for (int to = 0; to < n; ++to) {
                    for (int from = 0; from < n; ++from) {
                        if (from == to || flights[from][to])
                            dp[to] = max(dp[to], prev[from] + days[to][i]);
                    }
                }
            }
            
            int ans = 0;
            for (int v : dp)
                ans = max(ans, v);
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.