http://bookshadow.com/weblog/2017/04/30/leetcode-maximum-vacation-days/
http://www.cnblogs.com/grandyang/p/6919389.html
Solution 2. DP.
https://www.jianshu.com/p/a76ada51ac9e
X. DFS
https://www.jianshu.com/p/a76ada51ac9e
之前提到了递归的DFS会TLE,但是如果我们使用一个memo数组来保存中间计算结果,就能省去大量的重复计算,并且能够通过OJ,解题思想跟解法一非常的类似
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.
Rules and restrictions:
- You can only travel among N cities, represented by indexes from 0 to N-1. Initially, you are in the city indexed 0 on Monday.
- The cities are connected by flights. The flights are represented as a N*N matrix (not necessary symmetrical), called flights representing the airline status from the city i to the city j. If there is no flight from the city i to the city j, flights[i][j] = 0; Otherwise, flights[i][j] = 1. Also, flights[i][i] = 0 for all i.
- You totally have K weeks (each week has 7 days) to travel. You can only take flights at most once per day and can only take flights on each week's Monday morning. Since flight time is so short, we don't consider the impact of flight time.
- For each city, you can only have restricted vacation days in different weeks, given an N*K matrix called days representing this relationship. For the value of days[i][j], it represents the maximum days you could take vacation in the city i in the week j.
You're given the flights matrix and days matrix, and you need to output the maximum vacation days you could take during K weeks.
Example 1:
Example 2:
Example 3:
Note:
- N and K are positive integers, which are in the range of [1, 100].
- In the matrix days, all the values are integers in the range of [0, 1].
- In the matrix flights, all the values are integers in the range [0, 7].
- You could stay at a city beyond the number of vacation days, but you should work on the extra days, which won't be counted as vacation days.
- If you fly from the city A to the city B and take the vacation on that day, the deduction towards vacation days will count towards the vacation days of city B in that week.
- We don't consider the impact of flight hours towards the calculation of vacation days.
题目大意:
给定N个城市,K周时间。
矩阵flights描述N个城市之间是否存在航班通路。
若flights[i][j] = 1,表示i与j存在通路,否则表示不存在。特别的,flights[i][i]恒等于0。
矩阵days表示可以在某城市逗留的最长天数。
例如days[i][j] = k,表示第i个城市第j周最长可以逗留k天。
初始位于0号城市,每周可以选择一个能够到达的城市逗留(也可以留在当前城市)。
求最优策略下的最长逗留总天数。
注意:
- N和K是正整数,范围[1, 100]
- 矩阵flights的元素范围[0, 1]
- 矩阵days的元素范围[0, 7]
解题思路:
动态规划(Dynamic Programming)
状态转移方程:
https://segmentfault.com/a/1190000016980728
https://github.com/Cee/Leetcode/blob/master/568%20-%20Maximum%20Vacation%20Days.java
https://github.com/jzysheep/LeetCode/blob/master/568.%20Maximum%20Vacation%20Days%20dp1.cpp
- week, city, prevCity
http://www.acgtun.com/leetcode/solution/Maximum%20Vacation%20Days/javahttps://github.com/Cee/Leetcode/blob/master/568%20-%20Maximum%20Vacation%20Days.java
https://github.com/jzysheep/LeetCode/blob/master/568.%20Maximum%20Vacation%20Days%20dp1.cpp
- week, city, prevCity
public int maxVacationDays(int[][] flights, int[][] days) {
// n cities, k weeks
// flights[i][j] == 1 means there's a flight from i to j
// days[i][j] means the j-th week max vacation in city i
// dp[i][j] means the max vacation to in city i in week j
int n = flights.length;
int k = days[0].length;
int[][] dp = new int[n][k];
for (int i = 0; i < n; i++) {
Arrays.fill(dp[i], -1);
}
dp[0][0] = days[0][0];
for (int i = 1; i < n; i++) {
if (flights[0][i] == 1) {
dp[i][0] = days[i][0];
}
}
for (int week = 1; week < k; week++) {
for (int dest = 0; dest < n; dest++) {
for (int depart = 0; depart < n; depart++) {
if (dp[depart][week - 1] != -1 && (depart == dest || flights[depart][dest] == 1)) { // we can optimize this
dp[dest][week] = Math.max(dp[dest][week], dp[depart][week - 1] + days[dest][week]);
}
}
}
}
int res = 0;
for (int city = 0; city < n; city++) {
res = Math.max(res, dp[city][k - 1]);
}
return res;
}
http://www.cnblogs.com/grandyang/p/6919389.html
这道题给了我们一个NxN的数组,表示城市i是否有飞机直达城市j,又给了我们一个NxK的数组days,表示在第j周能在城市i休假的天数,让我们找出一个行程能使我们休假的天数最大化。开始尝试写了个递归的暴力破解法,结果TLE了。其实这道题比较适合用DP来解,我们建立一个二维DP数组,其中dp[i][j]表示目前是第j周,并且在此时在城市i,总共已经获得休假的总日子数。我们采取从后往前更新的方式(不要问我为什么,因为从前往后更新的写法要复杂一些),我们从第k周开始往第一周遍历,那么最后结果都累加在了dp[i][0]中,i的范围是[0, n-1],找出其中的最大值就是我们能休息的最大假期数了。难点就在于找递推式了,我们想dp[i][j]表示的是当前是第j周并在城市i已经获得的休假总日子数,那么上一个状态,也就是j+1周(因为我们是从后往前更新),跟当前状态有何联系,上一周我们可能还在城市i,也可能在其他城市p,那么在其他城市p的条件是,城市p有直飞城市i的飞机,那么我们可以用上一个状态的值dp[p][j+1]来更新当前值dp[i][j],还要注意的是我们要从倒数第二周开始更新,因为倒数第一周没有上一个状态,还有就是每个状态dp[i][j]都初始化赋为days[i][j]来更新,这样一旦没有任何城市可以直飞当前城市,起码我们还可以享受当前城市的假期,最后要做的就是想上面所说在dp[i][0]中找最大值,下面的代码是把这一步融合到了for循环中,所以加上了一堆判断条件,我们也可以在dp数组整个更新结束之后再来找最大值,参见代码如下:
int maxVacationDays(vector<vector<int>>& flights, vector<vector<int>>& days) { int n = flights.size(), k = days[0].size(), res = 0; vector<vector<int>> dp(n, vector<int>(k, 0)); for (int j = k - 1; j >= 0; --j) { for (int i = 0; i < n; ++i) { dp[i][j] = days[i][j]; for (int p = 0; p < n; ++p) { if ((i == p || flights[i][p]) && j < k - 1) { dp[i][j] = max(dp[i][j], dp[p][j + 1] + days[i][j]); } if (j == 0 && (i == 0 || flights[0][i])) res = max(res, dp[i][0]); } } } return res; }
下面这种方法优化了空间复杂度,只用了一个一维的DP数组,其中dp[i]表示在当前周,在城市i时已经获得的最大假期数,并且除了第一个数初始化为0,其余均初始化为整型最小值,然后我们从第一周往后遍历,我们新建一个临时数组t,初始化为整型最小值,然后遍历每一个城市,对于每一个城市,我们遍历其他所有城市,看是否有飞机能直达当前城市,或者就是当前的城市,我们用dp[p] + days[i][j]来更更新dp[i],当每个城市都遍历完了之后,我们将t整个赋值给dp,然后进行下一周的更新,最后只要在dp数组中找出最大值返回即可,这种写法不但省空间,而且也相对简洁一些,很赞啊~
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 j = 0; j < k; ++j) { vector<int> t(n, INT_MIN); for (int i = 0; i < n; ++i) { for (int p = 0; p < n; ++p) { if (i == p || flights[p][i]) { t[i] = max(t[i], dp[p] + days[i][j]); } } } dp = t; } return *max_element(dp.begin(), dp.end()); }
Solution 2. DP.
dp[i][j]
stands for the max vacation days we can get in week i
staying in city j
. It's obvious that dp[i][j] = max(dp[i - 1][k] + days[j][i]) (k = 0...N - 1, if we can go from city k to city j)
. Also because values of week i
only depends on week i - 1
, so we can compress two dimensional dp
array to one dimension. Time complexity O(K * N * N) as we can easily figure out from the 3 level of loops. public int maxVacationDays(int[][] flights, int[][] days) {
int N = flights.length;
int K = days[0].length;
int[] dp = new int[N];
Arrays.fill(dp, Integer.MIN_VALUE);
dp[0] = 0;
for (int i = 0; i < K; i++) {
int[] temp = new int[N];
Arrays.fill(temp, Integer.MIN_VALUE);
for (int j = 0; j < N; j++) {
for(int k = 0; k < N; k++) {
if (j == k || flights[k][j] == 1) {
temp[j] = Math.max(temp[j], dp[k] + days[j][i]);
}
}
}
dp = temp;
}
int max = 0;
for (int v : dp) {
max = Math.max(max, v);
}
return max;
}
Similar idea by using DP. The main difference is that I use DP backward.
The problem boils down to solving the following equation:
The problem boils down to solving the following equation:
V[i, k] = max( days[i][k] + V[i, k+1], days[j][k] + V[j][k+1])
, where j
satisfies j != i && flights[i][j] == 1
, for i = 0, ... ,N-1, k = 0, ... , K-1
, and V[i, k]
represent the maximum vocation days in City i
begining from Week k
.public int maxVacationDays(int[][] flights, int[][] days) {
if (days.length == 0 || flights.length == 0) return 0;
int N = flights.length;
int K = days[0].length;
int[][] vocationDays = new int[N][K + 1];
for (int k = K - 1; k >= 0; k--) {
for (int i = 0; i < N; i++) {
vocationDays[i][k] = days[i][k] + vocationDays[i][k + 1];
for (int j = 0; j < N; j++) {
if (flights[i][j] == 1) {
vocationDays[i][k] = Math.max(days[j][k] + vocationDays[j][k + 1], vocationDays[i][k]);
}
}
}
}
return vocationDays[0][0];
}
public int maxVacationDays(int[][] flights, int[][] days) {
int N = flights.length;
int K = days[0].length;
int[][] dp = new int[N][K];
boolean[][] canReach = new boolean[N][K];
int res = 0;
// init : week1 start from city 0
for (int i = 0; i < N; i++) {
if (i == 0 || flights[0][i] == 1) {
// System.out.printf(" we can fly from city %d to %d in week %d\n", 0, i, 0);
dp[i][0] = days[i][0];
canReach[i][0] = true;
}
}
// dp
for (int j = 1; j < K; j++) {
for (int i = 0; i < N; i++) {
for (int k = 0; k < N; k++) {
// we can fly from city k to i
if ((k == i || flights[k][i] == 1) && canReach[k][j - 1]) {
// System.out.printf(" we can fly from city %d to %d in week %d\n", k, i, j);
dp[i][j] = Math.max(dp[i][j], dp[k][j - 1] + days[i][j]);
canReach[i][j] = true;
}
}
}
}
for (int i = 0; i < N; i++) {
res = Math.max(res, dp[i][K - 1]);
}
return res;
}
X. DP - save spacehttps://www.jianshu.com/p/a76ada51ac9e
用dp[i][j]来表示 week i in city j, 最多可以得到多少个vacation
注意:一定要设置 dp[0] = 0;//this is very important, all should fly from city 0。然后其他设置为Integer.MIN_VALUE, 表示前后顺序,不从0出发都是不可接受的(在Math.max不会胜出)。
时间复杂度O(KNN)
dp[i][j] = max(dp[i - 1][k] + days[j][i]) (k = 0...N - 1, if we can go from city k to city j)
并且i的状态只跟i-1有关,于是我们可以把它转为循环数组。注意:一定要设置 dp[0] = 0;//this is very important, all should fly from city 0。然后其他设置为Integer.MIN_VALUE, 表示前后顺序,不从0出发都是不可接受的(在Math.max不会胜出)。
时间复杂度O(KNN)
public int maxVacationDays(int[][] flights, int[][] days) {
int N = flights.length;
int K = days[0].length;
int[] dp = new int[N];
Arrays.fill(dp, Integer.MIN_VALUE);
dp[0] = 0;
for (int i = 0; i < K; i++) {
int[] temp = new int[N];
Arrays.fill(temp, Integer.MIN_VALUE);
for (int j = 0; j < N; j++) {
for(int k = 0; k < N; k++) {
if (j == k || flights[k][j] == 1) {
temp[j] = Math.max(temp[j], dp[k] + days[j][i]);
}
}
}
dp = temp;
}
int max = 0;
for (int v : dp) {
max = Math.max(max, v);
}
return max;
}
Speed up
节约了K次Arrays.fill
X. DFS
https://www.jianshu.com/p/a76ada51ac9e
int max = 0, N = 0, K = 0;
public int maxVacationDays(int[][] flights, int[][] days) {
N = flights.length;
K = days[0].length;
dfs(flights, days, 0, 0, 0);
return max;
}
//curr: current city
private void dfs(int[][] f, int[][] d, int curr, int week, int sum) {
if (week == K) {
max = Math.max(max, sum);
return;
}
for (int dest = 0; dest < N; dest++) {
if (curr == dest || f[curr][dest] == 1) {
dfs(f, d, dest, week + 1, sum + d[dest][week]);
}
}
}
Solution 1. DFS. The idea is just try each possible
city for every week and keep tracking the max
vacation days. Time complexity O(N^K). Of course it will TLE....
int max = 0, N = 0, K = 0;
public int maxVacationDays(int[][] flights, int[][] days) {
N = flights.length;
K = days[0].length;
dfs(flights, days, 0, 0, 0);
return max;
}
private void dfs(int[][] f, int[][] d, int curr, int week, int sum) {
if (week == K) {
max = Math.max(max, sum);
return;
}
for (int dest = 0; dest < N; dest++) {
if (curr == dest || f[curr][dest] == 1) {
dfs(f, d, dest, week + 1, sum + d[dest][week]);
}
}
}
之前提到了递归的DFS会TLE,但是如果我们使用一个memo数组来保存中间计算结果,就能省去大量的重复计算,并且能够通过OJ,解题思想跟解法一非常的类似
int maxVacationDays(vector<vector<int>>& flights, vector<vector<int>>& days) { int n = flights.size(), k = days[0].size(); vector<vector<int>> memo(n, vector<int>(k, 0)); return helper(flights, days, 0, 0, memo); } int helper(vector<vector<int>>& flights, vector<vector<int>>& days, int city, int day, vector<vector<int>>& memo) { int n = flights.size(), k = days[0].size(), res = 0; if (day == k) return 0; if (memo[city][day] > 0) return memo[city][day]; for (int i = 0; i < n; ++i) { if (i == city || flights[city][i] == 1) { res = max(res, days[i][day] + helper(flights, days, i, day + 1, memo)); } } return memo[city][day] = res; }