share my java solution!


  • 0
    T
    public class Solution {
        public int maxVacationDays(int[][] flights, int[][] days) {
            int N=days.length;
            int K=days[0].length;
            
            int[][] dp=new int[N][K];
            int res=0;
            
            dfs(flights, days, dp, 0, 0);
            
            // for(int i=0;i<dp.length;i++){
            //     res=Math.max(res,dp[i][0]);
            // }
            
            return dp[0][0];
        }
        
        public int dfs(int[][] flights, int[][] days,int[][] dp, int week,int cur_city){
            if(week==days[0].length){
                return 0;
            }else if(dp[cur_city][week]!=0){
                return dp[cur_city][week];
            }else{
                int res=0;
                for(int j=0;j<flights.length;j++){
                    if(cur_city==j || flights[cur_city][j]==1){
                        res=Math.max(res,dfs(flights,days,dp,week+1,j)+days[j][week]);
                    }
                }
                dp[cur_city][week]=res;
                return dp[cur_city][week];
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.