Skip to content

Commit f7294be

Browse files
refactor 568
1 parent 5eb30e3 commit f7294be

File tree

1 file changed

+25
-21
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+25
-21
lines changed

src/main/java/com/fishercoder/solutions/_568.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,35 @@ You totally have K weeks (each week has 7 days) to travel.
7474
*/
7575
public class _568 {
7676

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);
8287
}
83-
return dfs(flights, days, 0, 0, memo);
84-
}
8588

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+
}
98102
}
103+
memo[curCity][weekno] = maxvac;
104+
return maxvac;
99105
}
100-
memo[curCity][weekno] = maxvac;
101-
return maxvac;
102106
}
103107

104108
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy