Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode solution in C++

1
class Solution {
2
public:
3
bool timeDiff(vector<string> timestamp, int index) {
4
int time1 = ((timestamp[index][0] - '0') * 10 + (timestamp[index][1] - '0')) * 60 +
5
((timestamp[index][3] - '0') * 10 + (timestamp[index][4] - '0'));
6
int time2 = ((timestamp[index + 2][0] - '0') * 10 + (timestamp[index + 2][1] - '0')) * 60 +
7
((timestamp[index + 2][3] - '0') * 10 + (timestamp[index + 2][4] - '0'));
8

9
if (abs(time2 - time1) <= 60) return true;
10
return false;
11
}
12

13
vector<string> alertNames(vector<string> &keyName, vector<string> &keyTime) {
14
unordered_map<string, vector<string>> ht;
15

16
for (int i = 0; i < keyName.size(); i++) ht[keyName[i]].push_back(keyTime[i]);
17

18
vector<string> result;
19
for (auto time : ht) {
20
sort(time.second.begin(), time.second.end());
21
for (int i = 0; i + 2 < time.second.size(); i++)
22
if (timeDiff(time.second, i)) {
23
result.push_back(time.first);
24
break;
25
}
26
}
27

28
sort(result.begin(), result.end());
29
return result;
30
}
31
};
32

33
/*
34
Algorithm Outline
35
1. store in HT as vector
36
2. Check for each, 1st & 3rd in range. True if Yes
37
*/

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0