So I just had a programming test for an interview and I consider myself a decent programmer, however I was unable to meet time constraints on the online test (and there was
Detect local maximums while running through the instructions. The answer will be the maximum of local maximums. Please, note that the vector must be sorted by start.
struct Instruction
{
size_t start, stop;
value_t value;
bool operator > (const Instruction& b) const
{
return stop > b.stop;
}
};
template
value_t do_work(const Ti&& b, const Ti&& e)
{
value_t result = 0;
value_t local = 0;
auto q = priority_queue,
greater>();
for (auto i=b; i!=e; ++i)
{
q.push(*i);
if (q.top().stop < i->start)
{
if (local > result)result = local;
do
{
local -= q.top().value;
q.pop();
} while (q.top().stop < i->start);
}
local += i->value;
}
return max(local, result);
}
int main()
{
vector p = {
{0,3,143}, {2,4,100}, {2,2,100},
{3,5,1000}, {4,4,500}
};
cout << do_work(begin(p),end(p)) << endl;
return 0;
}