I crashed again in this biweekly contest. Life really is full of ups and downs and then more downs… Problems 3 and 4 were actually still possible to solve, but my state during the contest was bad and I made the wrong decision. After looking at problem 3 for two minutes without an idea, I switched to problem 4. Then I overcomplicated problem 4, spent quite a bit of time implementing it, and still got stuck with TLE in the end.
1603. Design Parking System
A warm-up problem. Just maintain the number of remaining parking spots for each type.
/** * Your ParkingSystem object will be instantiated and called as such: * ParkingSystem* obj = new ParkingSystem(big, medium, small); * bool param_1 = obj->addCar(carType); */
Time complexity: O(addCar calls), space complexity: O(1).
1604. Alert Using Same Key-Card Three or More Times in a One Hour Period
Sort by time first, then traverse in order. Record each person’s entry times and check whether the last three entries fall within the same hour.
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()}; } };
Time complexity: O(N log N * nameLength), space complexity: O(N).
1605. Find Valid Matrix Given Row and Column Sums
Greedy. For each position, choose the smaller of the row sum and column sum. It can be proven that this will always produce a valid solution in the end.