A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1604. Alert Using Same Key-Card Three or More Times in a One Hour Period, the solution in this repository is mainly a greedy solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are time2int, diffInOneHour, alertNames.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 420 ms, faster than 100.00% of C++ online submissions for Alert Using Same Key-Card Three or More Times in a One Hour Period.
02//Memory Usage: 118.4 MB, less than 25.00% of C++ online submissions for Alert Using Same Key-Card Three or More Times in a One Hour Period.
03class Solution {
04public:
05 int time2int(string& t){
06 // cout << t << endl;
07 return stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3));
08 }
09
10 bool diffInOneHour(string& t1, string& t2){
11 return time2int(t2) - time2int(t1) <= 60;
12 }
13
14 vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime) {
15 unordered_map<string, vector<string>> name2times;
16
17 int n = keyName.size();
18
19 for(int i = 0; i < n; ++i){
20 name2times[keyName[i]].push_back(keyTime[i]);
21 }
22
23 vector<string> ans;
24
25 for(auto it = name2times.begin(); it != name2times.end(); ++it){
26 vector<string>& times = it->second;
27 sort(times.begin(), times.end());
28 for(int i = 2; i < times.size(); ++i){
29 if(diffInOneHour(times[i-2], times[i])){
30 ans.push_back(it->first);
31 break;
32 }
33 }
34 }
35
36 sort(ans.begin(), ans.end());
37
38 return ans;
39 }
40};
Cost