|
| 1 | +publicclassSolution { |
| 2 | +publicintminimumTime(intn,int[][]relations,int[]time) { |
| 3 | +Map<Integer,List<Integer>>adj =newHashMap<>(); |
| 4 | +for (int[]relation :relations) { |
| 5 | +intsrc =relation[0]; |
| 6 | +intdst =relation[1]; |
| 7 | +adj.putIfAbsent(src,newArrayList<>()); |
| 8 | +adj.get(src).add(dst); |
| 9 | + } |
| 10 | + |
| 11 | +Map<Integer,Integer>maxTime =newHashMap<>(); |
| 12 | + |
| 13 | +for (inti =1;i <=n;i++) { |
| 14 | +dfs(i,time,adj,maxTime); |
| 15 | + } |
| 16 | + |
| 17 | +returnmaxTime.values().stream().max(Integer::compareTo).get(); |
| 18 | + } |
| 19 | + |
| 20 | +privateintdfs(intsrc,int[]time,Map<Integer,List<Integer>>adj,Map<Integer,Integer>maxTime) { |
| 21 | +if (maxTime.containsKey(src)) { |
| 22 | +returnmaxTime.get(src); |
| 23 | + } |
| 24 | + |
| 25 | +intres =time[src -1]; |
| 26 | +for (intnei :adj.getOrDefault(src,newArrayList<>())) { |
| 27 | +res =Math.max(res,time[src -1] +dfs(nei,time,adj,maxTime)); |
| 28 | + } |
| 29 | +maxTime.put(src,res); |
| 30 | +returnres; |
| 31 | + } |
| 32 | +} |