Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I started taking the Algorithm design class from Coursera. So into the third week they talk about Greedy algorithms and walks through a sample problem.

This is the Problem Statement that I could find which was close.

Professor Midas drives an automobile from Newark to Reno along Interstate 80. His car’s gas tank, when full, holds enough gas to travel ‘d’ miles, and his map gives the distances between gas stations on his route. The professor wishes to make as few stops as possible along the way. The problem is to find an efficient method by which Professor Midas can determine at which gas stations he should stop, and prove that the strategy adopted yields an optimal solution.

The INPUT: an Array containing the Distance of each gas station from the Starting Point , and an integer that contains the maximum distance that the car can go with full tank of gas.

OUTPUT: The Minimum Number of Stops that the Car must make at each gas station to make a successful trip.

Here's the Code

   /*
    * Week 3 , Algorythm Design Course Coursera
    *
    * Design an algorythm that computes the minimum Number of Refills that a car must make to reach
    * the destination .
    *
    * INPUT :
    *       An Integer Array giving the distance of the GasStations from the Starting point ,
    *       An Integer that gives the maximum distance that the car can travel on a full refill
    *
    *  OUTPUT:
    *      The Minimum Number of refills , that the car must make.
    */

   var getLeastRefills = function(stationDist, maxDistance) {
     var curPos = 1;
     var minRefill = 0;
     var curDistance = maxDistance;
     var numRefillStations = (stationDist.length - 1);

     while (curPos <= (numRefillStations)) {
       for (; stationDist[curPos] <= curDistance; curPos += 1);

       if (curPos > numRefillStations) return minRefill;

       curPos -= 1;
       minRefill += 1;
       curDistance = stationDist[curPos] + maxDistance;
     }

     if (stationDist[curPos] < curDistance)
       minRefill += 1;

     return minRefill;
   };

    // Test Cases
   console.log(getLeastRefills([0, 200, 375, 550, 750, 950], 400));

share|improve this question
    
Just in case you didn't notice, your project question asks for an array of stops to refill out as output. As for a way to help improve your code, your solution is already O(n) which is just about as good as it gets (Good work!). Hyper-optimizations that only improve your code by a constant factor generally aren't worth the time to program, especially in such a high level language as JavaScript. – Josh Dawson Jun 8 '16 at 4:42
    
The Code worked for One or two test cases . But i was unsure whether it really works. Thanks. – Aswin Mohan Jun 9 '16 at 4:10
    
One problem your code has is that it goes into an infinite loop when there is no solution (specifically if there is ever a distance greater than the tank size: [0, 100, 700, 950]). This can be annoying for someone testing out your code (me having to close my web browser from the task manager). – Josh Dawson Jun 9 '16 at 4:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.