|
| 1 | +class Solution { |
| 2 | + public int earliestFinishTime(int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) { |
| 3 | + int result = Integer.MAX_VALUE; |
| 4 | + int n = landStartTime.length; |
| 5 | + int m = waterStartTime.length; |
| 6 | + int minEnd = Integer.MAX_VALUE; |
| 7 | + // Land ride first |
| 8 | + for (int i = 0; i < n; i++) { |
| 9 | + minEnd = Math.min(minEnd, landStartTime[i] + landDuration[i]); |
| 10 | + } |
| 11 | + for (int i = 0; i < m; i++) { |
| 12 | + result = Math.min(result, waterDuration[i] + Math.max(minEnd, waterStartTime[i])); |
| 13 | + } |
| 14 | + minEnd = Integer.MAX_VALUE; |
| 15 | + // Water ride first |
| 16 | + for (int i = 0; i < m; i++) { |
| 17 | + minEnd = Math.min(minEnd, waterStartTime[i] + waterDuration[i]); |
| 18 | + } |
| 19 | + for (int i = 0; i < n; i++) { |
| 20 | + result = Math.min(result, landDuration[i] + Math.max(minEnd, landStartTime[i])); |
| 21 | + } |
| 22 | + return result; |
| 23 | + } |
| 24 | +} |
0 commit comments