# 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 -> so N people
// people are in a queue , find total number of stops
int solution(const int &N,const std::vector<int> &weight,const std::vector<int> &floor)
{
int idx = 0;
float last_weight = 0.0;
int last_count = 0;
std::set<int> stops;
int stop_count = 0;
while(idx < N)
{
//lift full, dont put any more ppl
if(last_count + 1 > max_number_of_ppl || last_weight + weight[idx] > max_weight)
{
stop_count += static_cast<int>(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() >0)
{
stop_count += static_cast<int>(stops.size());
}

return stop_count;
}


Simple test:

int main()
{
int N = 6;
std::vector<int> weight;
std::vector<int> 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<<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<int>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.