|
| 1 | +classSolution { |
| 2 | +publicintearliestFinishTime(int[]landStartTime,int[]landDuration,int[]waterStartTime,int[]waterDuration) { |
| 3 | +intresult =Integer.MAX_VALUE; |
| 4 | +intn =landStartTime.length; |
| 5 | +intm =waterStartTime.length; |
| 6 | +intminEnd =Integer.MAX_VALUE; |
| 7 | +// Land ride first |
| 8 | +for (inti =0;i <n;i++) { |
| 9 | +minEnd =Math.min(minEnd,landStartTime[i] +landDuration[i]); |
| 10 | + } |
| 11 | +for (inti =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 (inti =0;i <m;i++) { |
| 17 | +minEnd =Math.min(minEnd,waterStartTime[i] +waterDuration[i]); |
| 18 | + } |
| 19 | +for (inti =0;i <n;i++) { |
| 20 | +result =Math.min(result,landDuration[i] +Math.max(minEnd,landStartTime[i])); |
| 21 | + } |
| 22 | +returnresult; |
| 23 | + } |
| 24 | +} |