Advertisement
lifeiteng

568. Maximum Vacation Days

Sep 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. class Solution {
  2.     public int maxVacationDays(int[][] flights, int[][] days) {
  3.         int n = flights.length, k = days[0].length;
  4.         int[][] f = new int[k + 1][n];
  5.         for(int i = 0; i < n; i++)
  6.         {
  7.             f[k][i] = days[i][k - 1];    // base case
  8.             flights[i][i] = 1;
  9.         }
  10.         for(int d = k - 1; d >= 1; d--)
  11.         {
  12.             for(int i = 0; i < n; i++)
  13.             {
  14.                 for(int j = 0; j < n; j++)
  15.                 {
  16.                     if(flights[i][j] == 0) continue;
  17.                     f[d][i] = Math.max(f[d][i], f[d + 1][j] + days[i][d - 1]);
  18.                 }
  19.             }
  20.         }
  21.         // for(int[] ff : f) System.out.println(Arrays.toString(ff));
  22.         int max = 0;
  23.         for(int i = 0; i < n; i++)
  24.         {
  25.             if(flights[0][i] == 1) max = Math.max(max, f[1][i]);
  26.         }
  27.         return max;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement