I've modified the code as below for a sample test as given in http://ift.tt/2sYBx46
"A TrainComposition is built by attaching and detaching wagons from the left and the right sides. For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13. Implement a TrainComposition that models this problem."
#include <stdexcept>
#include <iostream>
#include <vector>
class TrainComposition
{
//std::vector<int> wagons;
public:
std::vector<int> wagons;
void attachWagonFromLeft(int wagonId)
{
wagons.insert(wagons.begin(), wagonId);
}
void attachWagonFromRight(int wagonId)
{
wagons.push_back(wagonId);
}
int detachWagonFromLeft()
{
int ret = wagons[0];
wagons.erase(wagons.begin());
return ret;
}
int detachWagonFromRight()
{
int ret = wagons[(int)(wagons.size()) - 1];
wagons.pop_back();
return ret;
}
};
#ifndef RunTests
int main()
{
TrainComposition tree;
tree.attachWagonFromLeft(7);
tree.attachWagonFromLeft(13);
std::cout << tree.detachWagonFromRight() << "\n"; // 7
std::cout << tree.detachWagonFromLeft() << "\n";; // 13
return 0;
}
#endif
The test result has one fail as Performance test with a large number of wagons: Time limit exceeded
Please help to fix this fail...
Aucun commentaire:
Enregistrer un commentaire