初级农民-请到新手上路获取积分
- 积分
- 9
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-12-28
- 最后登录
- 1970-1-1
|
- class TimeNode {
- public:
- int time;
- bool start;
-
- public:
- TimeNode(int time, bool start) : time(time), start(start) {}
-
- bool operator < (const TimeNode &t) const {
- return this->time < t.time;
- }
- };
- class Solution {
- public:
- vector<string> statistics(const vector<Employee> &employees) {
- // base case
- vector<string> result;
- if (employees.empty()) return result;
-
- // sort
- vector<TimeNode> times;
- for (const auto employee : employees) {
- times.push_back(TimeNode(employee.startTime, true));
- times.push_back(TimeNode(employee.endTime, false));
- }
- sort(times.begin(), times.end());
-
- // scan
- int i = 1, count = 1, begin = 0;
- while (i < times.size()) {
- if (times[i].time != times[i - 1].time) {
- result.push_back(make_statistics(times[begin].time, times[i].time, count));
- begin = i;
- }
- count += times[i].start? 1: -1;
- i++;
- }
-
- return result;
- }
-
- string make_statistics(int start, int end, int count) {
- return to_string(start) + "->" + to_string(end) + ": " + to_string(count);
- }
- };
- int main(int argc, const char * argv[]) {
- vector<Employee> employees;
- employees.push_back(Employee(4, 2004, 2015));
- employees.push_back(Employee(7, 2011, 2015));
- employees.push_back(Employee(9, 2004, 2009));
- employees.push_back(Employee(11, 2007, 2008));
- employees.push_back(Employee(12, 2008, 2014));
- employees.push_back(Employee(15, 2013, 2014));
- for (const auto str : Solution().statistics(employees)) {
- cout << str << endl;
- }
-
- return 0;
- }
复制代码 |
|