@@ -74,31 +74,35 @@ You totally have K weeks (each week has 7 days) to travel.
74
74
*/
75
75
public class _568 {
76
76
77
- /**credit: https://leetcode.com/articles/maximum-vacation-days/#approach-2-using-dfs-with-memoization-accepted*/
78
- public int maxVacationDays (int [][] flights , int [][] days ) {
79
- int [][] memo = new int [flights .length ][days [0 ].length ];
80
- for (int [] l : memo ) {
81
- Arrays .fill (l , Integer .MIN_VALUE );
77
+ public static class Solution1 {
78
+ /**
79
+ * credit: https://leetcode.com/articles/maximum-vacation-days/#approach-2-using-dfs-with-memoization-accepted
80
+ */
81
+ public int maxVacationDays (int [][] flights , int [][] days ) {
82
+ int [][] memo = new int [flights .length ][days [0 ].length ];
83
+ for (int [] l : memo ) {
84
+ Arrays .fill (l , Integer .MIN_VALUE );
85
+ }
86
+ return dfs (flights , days , 0 , 0 , memo );
82
87
}
83
- return dfs (flights , days , 0 , 0 , memo );
84
- }
85
88
86
- public int dfs (int [][] flights , int [][] days , int curCity , int weekno , int [][] memo ) {
87
- if (weekno == days [0 ].length ) {
88
- return 0 ;
89
- }
90
- if (memo [curCity ][weekno ] != Integer .MIN_VALUE ) {
91
- return memo [curCity ][weekno ];
92
- }
93
- int maxvac = 0 ;
94
- for (int i = 0 ; i < flights .length ; i ++) {
95
- if (flights [curCity ][i ] == 1 || i == curCity ) {
96
- int vac = days [i ][weekno ] + dfs (flights , days , i , weekno + 1 , memo );
97
- maxvac = Math .max (maxvac , vac );
89
+ public int dfs (int [][] flights , int [][] days , int curCity , int weekno , int [][] memo ) {
90
+ if (weekno == days [0 ].length ) {
91
+ return 0 ;
92
+ }
93
+ if (memo [curCity ][weekno ] != Integer .MIN_VALUE ) {
94
+ return memo [curCity ][weekno ];
95
+ }
96
+ int maxvac = 0 ;
97
+ for (int i = 0 ; i < flights .length ; i ++) {
98
+ if (flights [curCity ][i ] == 1 || i == curCity ) {
99
+ int vac = days [i ][weekno ] + dfs (flights , days , i , weekno + 1 , memo );
100
+ maxvac = Math .max (maxvac , vac );
101
+ }
98
102
}
103
+ memo [curCity ][weekno ] = maxvac ;
104
+ return maxvac ;
99
105
}
100
- memo [curCity ][weekno ] = maxvac ;
101
- return maxvac ;
102
106
}
103
107
104
108
}
0 commit comments