Advertisement
darekfive

Minimum Total Cost W/ Minimum Days Between Flights

Mar 14th, 2025
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1.  public static int minimumTotalCost(int[] departures, int[] returns, int minGap) {
  2.         int res = Integer.MAX_VALUE;
  3.         int minDeparture = Integer.MAX_VALUE; // Tracks minimum departure in valid window
  4.  
  5.         for (int R = 0; R < departures.length; R++) {
  6.             // Only update minDeparture when we reach a valid window
  7.             if (R >= minGap) {
  8.                 minDeparture = Math.min(minDeparture, departures[R - minGap]);
  9.                 res = Math.min(res, returns[R] + minDeparture);
  10.             }
  11.         }
  12.  
  13.         return res == Integer.MAX_VALUE ? -1 : res;
  14.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement