在尝试提高我的算法技能时,我发现自己陷入了以下问题,简而言之,要求您找到时间段的持续时间,其中房间中有最多人数:
https://jutge.org/problems/P27158_en
我提出的解决方案正确解决了网站建议的所有公共测试用例的问题,但是对于一个或多个隐藏的私有测试用例失败了.
我的解决方案为std :: vector中的每个事件保存了两个条目:一个用于到达,一个用于离开,每个条目由[eventtype,eventtime]组成.然后按事件时间对向量进行排序,最后循环遍历向量以确定有最大访客数的时间跨度的持续时间.我的代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
enum class EventType {ARRIVE, LEAVE};
struct Event{
int time;
EventType type;
Event(){}
Event(int tm, EventType t) : time(tm), type (t){}
// inline bool operator<(const Event& e) const {return time < e.time || (time == e.time && type==EventType::LEAVE);}
};
bool eventCompare(const Event& e1, const Event& e2) {
if(e1.time < e2.time){
return true;
} else if(e1.time == e2.time) {
if(e1.type == EventType::LEAVE && e2.type == EventType::ARRIVE) { …Run Code Online (Sandbox Code Playgroud)