Total number of stops for an elevator to serve certain people I have seen this question asked around a bit, such as this Code Review question in Java:


  Write an optimal program that would determine the total number of stops
  a elevator has taken to serve X number of people. There is a elevator
  in a building with M floors. This elevator can take a max of X people
  at a time or max of total weight Y. Given that a set of people has
  arrived and their weight and the floor they need to stop given how
  many stops has the elevator taken to serve all the people. Consider
  elevator serves in the first come first serve basis. (N people in Queue)


I am making this code review request to see if I can get any feedback on my C++ implementation.

const int max_number_of_ppl = 3; // X
const float max_weight = 250.0;  // Y
const int number_of_floors = 5; // M

// assume person array is 0,1,....N-1 -&gt; so N people
// people are in a queue , find total number of stops 
int solution(const int &amp;N,const std::vector&lt;int&gt; &amp;weight,const std::vector&lt;int&gt; &amp;floor)
{
    int idx = 0;
    float last_weight = 0.0;
    int last_count = 0;
    std::set&lt;int&gt; stops;
    int stop_count = 0;
    while(idx &lt; N)
    {
        //lift full, dont put any more ppl
        if(last_count + 1 &gt; max_number_of_ppl || last_weight + weight[idx] &gt; max_weight)
        {
            stop_count += static_cast&lt;int&gt;(stops.size());
            stops.clear();
            last_weight = 0.0;
            last_count = 0;

        }

        //fill lift with the person
        stops.insert(floor.at(idx));
        last_count++;
        last_weight += weight.at(idx);

        //fetch next person
        idx++;
    }

    //handle last lift session
    if(stops.size() &gt;0)
    {
        stop_count += static_cast&lt;int&gt;(stops.size());
    }

    return stop_count;
}


Simple test:

int main()
{
    int N = 6;
    std::vector&lt;int&gt; weight;
    std::vector&lt;int&gt; stops;
    weight.emplace_back(100);
    weight.emplace_back(70);
    weight.emplace_back(60);
    weight.emplace_back(190);
    weight.emplace_back(100);
    weight.emplace_back(10);

    stops.emplace_back(5);
    stops.emplace_back(5);
    stops.emplace_back(5);
    stops.emplace_back(4);
    stops.emplace_back(3);
    stops.emplace_back(3);

    std::cout&lt;&lt;solution(N,weight,stops);

    return 0;
}
First of all I'd like to suggest you use a for-loop, since it would be easier to see, that you're going to increment the loop variable in every iteration independent of any conditions. Then you could also exchange your idx variable for i since the first does not convey more or less semantics than the second and it is somewhat unwritten convention. 

Next: Why use a static_cast&lt;int&gt;for incrementing the stop_count? I think you would not need that, if you defined stop_count as a size_t.

Also: Was there some sort of requirement that solution() takes N as an argument? You could get rid of that parameter, since you have the weight-vector, which in turn has a size().

Furthermore: What is the parameter M (number_of_floors) going to be used for? I do not see much use for it other than boundary checking in the program, in a real world scenario no one could press button 10 in a 5-story building anyway.