-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathInvalid Transactions (LeetCode).cpp
88 lines (70 loc) · 2.2 KB
/
Invalid Transactions (LeetCode).cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class transaction {
public:
string name;
int time;
int amount;
string city;
// Parametrised Constructor
transaction(string name, int time, int amount, string city)
{
this->name = name;
this->time = time;
this->amount = amount;
this->city = city;
}
};
class Solution {
public:
vector<string> invalidTransactions(vector<string>& transactions) {
// Vector of class 'transaction' to store the input effeciently
vector<transaction> input;
int n = transactions.size();
vector<bool> valid(n, true);
//extracting information from string
for (int i = 0; i < n; i++)
{
string curr = transactions[i];
string name = "", time = "", amount = "", city = "";
int j = 0;
while (curr[j] != ',')
name += curr[j++];
j++;
while (curr[j] != ',')
time += curr[j++];
j++;
while (curr[j] != ',')
amount += curr[j++];
j++;
while (j < curr.size())
city += curr[j++];
// Creating a object 't' of class transaction
transaction t(name, stoi(time), stoi(amount), city);
input.push_back(t);
}
//first check : amount > 1000
for (int i = 0; i < input.size(); i++)
if (input[i].amount > 1000)
valid[i] = false;
//second check, pair wise
for (int i = 0; i < (input.size()); i++)
{
for (int j = 0; j < input.size(); j++)
{
if (i == j)
continue;
int timediff = abs(input[j].time - input[i].time);
if (timediff <= 60)
{
if ((input[j].name == input[i].name) && (input[j].city != input[i].city))
valid[i] = false, valid[j] = false;
}
}
}
//Insert Invalid transactions to final answer
vector<string> ans;
for (int i = 0; i < input.size(); i++)
if (!valid[i])
ans.push_back(transactions[i]);
return ans;
}
};