Ad
Fundamentals
Arrays
Fundamentals
Arrays
Fundamentals
Arrays
Code
Diff
  • class Solution{
      
     public static int lastPoint(int fuel, int consumption, int[] stations) {
     
        if (fuel / consumption < stations[0]) {
            return -1;
        }
    
        int fuelLeft = fuel - stations[0] * consumption;
    
        for (int i = 1; i < stations.length; i++) {
            int distance = stations[i] - stations[i - 1];
    
            if (fuelLeft > distance * consumption) {
     
                fuelLeft =  fuelLeft - (distance * consumption);
            }
            else {
                return stations[i - 1];
                
            }
        }
         
        return stations[stations.length - 1]; 
    }
    
    }
    • class Solution{
    • public static int lastPoint(int fuel, int consumption, int[] stations) {
    • if (fuel / consumption < stations[0]) {
    • return -1;
    • }
    • int fuelLeft = fuel - stations[0] * consumption;
    • for (int i = 1; i < stations.length; i++) {
    • int distance = stations[i] - stations[i - 1];
    • fuelLeft = fuelLeft - distance * consumption;
    • if (fuelLeft < 0) {
    • if (fuelLeft > distance * consumption) {
    • fuelLeft = fuelLeft - (distance * consumption);
    • }
    • else {
    • return stations[i - 1];
    • }
    • }
    • return -1;
    • return stations[stations.length - 1];
    • }
    • }
Fundamentals
Arrays
Code
Diff
  • class Solution{
      
     public static int lastPoint(int fuel, int consumption, int[] stations) {
        if (fuel / consumption < stations[0]) {
            return -1;
        }
    
        int fuelLeft = fuel - stations[0] * consumption;
    
        for (int i = 1; i < stations.length; i++) {
            int distance = stations[i] - stations[i - 1];
    
            fuelLeft = fuelLeft - distance * consumption;
    
            if (fuelLeft < 0) {
                return stations[i - 1];
            }
        }
    
        return -1; 
    }
      
      
    }
    • class Solution{
    • public static int lastPoint (int fuel, int consumption, int[]stations){
    • //DO YOUR MAGIC!! YES
    • return -1;
    • }
    • public static int lastPoint(int fuel, int consumption, int[] stations) {
    • if (fuel / consumption < stations[0]) {
    • return -1;
    • }
    • int fuelLeft = fuel - stations[0] * consumption;
    • for (int i = 1; i < stations.length; i++) {
    • int distance = stations[i] - stations[i - 1];
    • fuelLeft = fuelLeft - distance * consumption;
    • if (fuelLeft < 0) {
    • return stations[i - 1];
    • }
    • }
    • return -1;
    • }
    • }