/** * Your ParkingSystem object will be instantiated and called as such: * ParkingSystem* obj = new ParkingSystem(big, medium, small); * bool param_1 = obj->addCar(carType); */
时间复杂度: O(addCar calls),
空间复杂度: O(1).
1604. Alert Using Same Key-Card Three or More Times in a One Hour Period
classSolution { public: vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime){ using pss = pair<string, string>; set<string> ans; constint n = keyName.size(); vector<pss> records; records.reserve(n); for (int i = 0; i < n; ++i) { records.emplace_back(keyTime[i], keyName[i]); } sort(records.begin(), records.end()); int hour = 0; unordered_map<string, vector<string>> enter; auto convert2int = [&](const string& x) -> int { int h = stoi(x.substr(0, 2)); int m = stoi(x.substr(3)); return h * 60 + m; }; auto check = [&](const string& x) -> bool { if (enter[x].size() < 3) returnfalse; else { int a = convert2int(enter[x][enter[x].size() - 1]); int b = convert2int(enter[x][enter[x].size() - 2]); int c = convert2int(enter[x][enter[x].size() - 3]); return a - c <= 60; } }; for (int i = 0; i < n; ++i) { enter[records[i].second].push_back(records[i].first); if (check(records[i].second)) { ans.insert(records[i].second); } } return {ans.begin(), ans.end()}; } };