classSolution { public: boolcanFormArray(vector<int>& arr, vector<vector<int>>& pieces){ unordered_map<int, int> num2indexInPieces; for (int i = 0; i < pieces.size(); ++i) { constauto& v = pieces[i]; num2indexInPieces[v[0]] = i; } for (int i = 0; i < arr.size(); ) { auto it = num2indexInPieces.find(arr[i]); if (it == num2indexInPieces.end()) returnfalse; constint j = it->second; constint oldi = i; for (; i < arr.size() && i - oldi < pieces[j].size(); ++i) { if (arr[i] != pieces[j][i - oldi]) returnfalse; } } returntrue; } };
classSolution { public: doubletrimMean(vector<int>& arr){ sort(arr.begin(), arr.end()); constint n = arr.size(); constint x = n / 20; double s = 0.0; for (int i = 0; i < n; ++i) { if (i < x || (n - 1 - i) < x) continue; s += arr[i]; } return s / (n - 2 * x); } };
/** * 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()}; } };