Sort by

recency

|

369 Discussions

|

  • + 0 comments

    My java 8 submission for all test passing (this code is not optimized, maybe some redundancy to make it obvious to the beginner readers). It passes all cases

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    
    
    class Result {
        public static boolean allPumpsVisited(int contPumpsVisited, List<List<Integer>> pumps) {
            return contPumpsVisited >= pumps.size();
        }
        public static int computeRemainingGasForNextDestination(List<Integer> row) {
            int petrol = row.get(0);
            int kmNextP = row.get(1);
            return (petrol - kmNextP);
        }
        
        public static boolean simulateTraverse(int currentPumpIdx, List<Integer> currentPumpObj, List<List<Integer>> pumps) {
            int start;
            int end;
            int lastPumpIdx = pumps.size() - 1;
            boolean isMiddleIndex = false;
            int contPumpsVisited = 1;
            // Initial petrol would be the delta from the current node to the next one
            int remainingGas = computeRemainingGasForNextDestination(currentPumpObj);
            boolean isLast = currentPumpIdx == lastPumpIdx;
            if (isLast) { // last node
                start = 0;
                end = lastPumpIdx - 1;
            } else { // first node
                start = currentPumpIdx + 1;
                end = lastPumpIdx;
                // middle
                isMiddleIndex = currentPumpIdx > 0;
            }
    
            for (int i=start; i <= end; i ++) {
                List<Integer> row = pumps.get(i);
                remainingGas+= computeRemainingGasForNextDestination(row);
                if (remainingGas >= 0) {
                    contPumpsVisited++;
                }  else {
                    // Whole circle cannot be completed. abort operation
                    return false;
                }
            }
    
    				//Some people to avoid doing this use the mod operator %
            if (isMiddleIndex) {
                // we will end at the node before the starting middle node
                end = currentPumpIdx  - 1;
                // Iterate the other remaining nodes to complete circle
                for (int i= 0; i <= end; i++) {
                    contPumpsVisited++;
                    // If we only are missing to visit the last node, we don't need to compute anything
                    // we have completed the circle already
                    if (allPumpsVisited(contPumpsVisited, pumps)) {
                        return true;
                    }
    
                    List<Integer> row = pumps.get(i);
                    remainingGas+= computeRemainingGasForNextDestination(row);
                    if (remainingGas >= 0) {
                        if (allPumpsVisited(contPumpsVisited, pumps)) {
                            return true;
                        } else {
                            continue;
                        }
                    }  else {
                        return false;
                    }
                }
    
            }
            // Evaluate if visited all nodes.
            return allPumpsVisited(contPumpsVisited, pumps);
        }
    
        public static int truckTour(List<List<Integer>> petrolpumps) {
       // Write your code here
            // Write your code here
            int noSolution = -1;
    
            for (int i=0; i < petrolpumps.size(); i++) {
                List<Integer> row = petrolpumps.get(i);
                int diffGas = computeRemainingGasForNextDestination(row);
                // Only if there is enough gas we chose that node
                if (diffGas >= 0) {
                    boolean courseCompleted = simulateTraverse(i, row, petrolpumps);
                    if ( courseCompleted ) {
                        return i;
                    }
                }
    
            }
    
            return noSolution;
        }
    ... 						
    }
    
    							
    							
    
  • + 0 comments

    This problem really reminds me of planning real-world routes — kind of like how logistics or travel planning works. It’s similar to when you’re exploring Top Attractions in Abu Dhabi, figuring out the most efficient way to visit all the must-see spots without wasting time or energy. Just like the truck finding the best starting point, it’s all about smart planning and balance. Really interesting discussion!

  • + 0 comments

    I recently came across a similar problem where I had to figure out the best starting point for a petrol pump tour on a circle. After some thinking, I realized that it's like solving a puzzle, much like in games. It reminded me of TruckersofEuropeAPK, a modified version of Truckers of Europe 3, where you're tasked with transporting cargo across European cities, just like the truck in this problem. In TruckersofEuropeAPK, all the premium features are unlocked, and it’s as if you’re calculating the best route in real life. Definitely a cool experience if you're into driving simulation games!

  • + 0 comments

    Save the previous circle you already calculated, in a way you can speed it up via memoization. After each pump you can add that pump's total cost to the cost of the pumps before it. Using this strategy you will never have to recalclate the beginning of the loop.

    There's probably other ways to optimize it, but I got lazy after memoizing the beginning part of the loop. This way you never have to worry about trying to loop back your array around.

  • + 0 comments

    Im getting an error with this:int fuel = 0; int miles = 0; int index = 0; for (int i = 0; i < petrolpumps.size(); i++) { fuel += petrolpumps.get(i).get(0); miles += petrolpumps.get(i).get(1); if (fuel < miles) { fuel = 0; miles = 0; index = i + 1; } } return index; However if I change the inequality to this "if (fuel - miles < 0)" ,it is passing all the test cases. They are both supposed to be the same according to linear inequalities. This is math defying. Please help.