forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_568.java
38 lines (33 loc) · 1.24 KB
/
_568.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.fishercoder.solutions;
import java.util.Arrays;
public class _568 {
public static class Solution1 {
/**
* credit: https://leetcode.com/articles/maximum-vacation-days/#approach-2-using-dfs-with-memoization-accepted
*/
public int maxVacationDays(int[][] flights, int[][] days) {
int[][] memo = new int[flights.length][days[0].length];
for (int[] l : memo) {
Arrays.fill(l, Integer.MIN_VALUE);
}
return dfs(flights, days, 0, 0, memo);
}
public int dfs(int[][] flights, int[][] days, int curCity, int weekno, int[][] memo) {
if (weekno == days[0].length) {
return 0;
}
if (memo[curCity][weekno] != Integer.MIN_VALUE) {
return memo[curCity][weekno];
}
int maxvac = 0;
for (int i = 0; i < flights.length; i++) {
if (flights[curCity][i] == 1 || i == curCity) {
int vac = days[i][weekno] + dfs(flights, days, i, weekno + 1, memo);
maxvac = Math.max(maxvac, vac);
}
}
memo[curCity][weekno] = maxvac;
return maxvac;
}
}
}