Skip to content

Commit 9005421

Browse files
authored
Create maximum-vacation-days.cpp
1 parent de6a5c6 commit 9005421

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/maximum-vacation-days.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(n^2 * k)
2+
// Space: O(k)
3+
4+
class Solution {
5+
public:
6+
int maxVacationDays(vector<vector<int>>& flights, vector<vector<int>>& days) {
7+
if (days.empty() || flights.empty()) {
8+
return 0;
9+
}
10+
vector<vector<int>> dp(2, vector<int>(days.size()));
11+
for (int week = days[0].size() - 1; week >= 0; --week) {
12+
for (int cur_city = 0; cur_city < days.size(); ++cur_city) {
13+
dp[week % 2][cur_city] = days[cur_city][week] + dp[(week + 1) % 2][cur_city];
14+
for (int dest_city = 0; dest_city < days.size(); ++dest_city) {
15+
if (flights[cur_city][dest_city] == 1) {
16+
dp[week % 2][cur_city] = max(dp[week % 2][cur_city],
17+
days[dest_city][week] + dp[(week + 1) % 2][dest_city]);
18+
}
19+
}
20+
}
21+
}
22+
return dp[0][0];
23+
}
24+
};

0 commit comments

Comments
 (0)