-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
65 lines (54 loc) · 1.71 KB
/
Copy pathsolution.cpp
File metadata and controls
65 lines (54 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution
{
public:
vector<string> validateCoupons(vector<string> &code,
vector<string> &businessLine,
vector<bool> &isActive)
{
// Priority order for business lines
unordered_map<string, int> priority = {
{"electronics", 0},
{"grocery", 1},
{"pharmacy", 2},
{"restaurant", 3}};
vector<pair<int, string>> validCoupons;
for (int i = 0; i < code.size(); i++)
{
// Check active status
if (!isActive[i])
continue;
// Check business line validity
if (priority.find(businessLine[i]) == priority.end())
continue;
// Check code validity
if (code[i].empty())
continue;
bool ok = true;
for (char c : code[i])
{
if (!isalnum(c) && c != '_')
{
ok = false;
break;
}
}
if (!ok)
continue;
validCoupons.push_back({priority[businessLine[i]], code[i]});
}
// Sort by business priority, then by code
sort(validCoupons.begin(), validCoupons.end(),
[](auto &a, auto &b)
{
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
});
vector<string> result;
for (auto &p : validCoupons)
{
result.push_back(p.second);
}
return result;
}
};