File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments