Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
A transaction is possibly invalid if:
$1000
, or;60
minutes of another transaction with the same name in a different city.You are given an array of strings transaction
where transactions[i]
consists of comma-separated values representing the name, time (in minutes), amount, and city of the transaction.
Return a list of transactions
that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
transactions[i]
takes the form "{name},{time},{amount},{city}"
{name}
and {city}
consist of lowercase English letters, and have lengths between 1
and 10
.{time}
consist of digits, and represent an integer between 0
and 1000
.{amount}
consist of digits, and represent an integer between 0
and 2000
.class Solution {
public List<String> invalidTransactions(final String[] transactions) {
final List<String> invalid = new ArrayList<>();
final Map<String, List<Transaction>> map = new HashMap<>();
/*
* build a map with name as key and value as list of transactions for that name
*/
for (final String transaction : transactions) {
final Transaction tran = new Transaction(transaction);
if (map.containsKey(tran.name)) {
map.get(tran.name).add(tran);
} else {
final List<Transaction> list = new ArrayList<>();
list.add(tran);
map.put(tran.name, list);
}
}
for (final String transaction : transactions) {
final Transaction tran = new Transaction(transaction);
if (!isValid(map.get(tran.name), tran)) {
invalid.add(transaction);
}
}
return invalid;
}
public boolean isValid(final List<Transaction> transactions, final Transaction transaction) {
/* if there is only one transaction and the amount is less than 1000 */
if (transactions.size() <= 1 && transaction.amount < 1000)
return true;
/* check against all other transaction to check it this one is valid */
for (final Transaction tran : transactions) {
if (transaction.invalidTransaction(tran.city, tran.time)) {
return false;
}
}
return true;
}
class Transaction {
String name;
int time;
int amount;
String city;
Transaction(final String transaction) {
final String[] t = transaction.split(",");
this.name = t[0];
this.time = Integer.parseInt(t[1]);
this.amount = Integer.parseInt(t[2]);
this.city = t[3];
}
/*
* the amount exceeds $1000, or;
*
* if it occurs within (and including) 60 minutes of another transaction with
* the same name in a different city. Each transaction string transactions[i]
* consists of comma separated values representing the name, time (in minutes),
* amount, and city of the transaction.
*/
public boolean invalidTransaction(final String city, final int time) {
return invalidAmount() || differentCity(city, time);
}
private boolean differentCity(final String city, final int time) {
return !this.city.equals(city) && Math.abs(this.time - time) <= 60;
}
private boolean invalidAmount() {
return this.amount > 1000;
}
}
}
class Solution(object):
def invalidTransactions(self, transactions):
"""
:type transactions: List[str]
:rtype: List[str]
"""
r = {}
inv = []
for i in transactions:
split = i.decode("utf-8").split(",")
name = str(split[0])
time = int(split[1])
amount = int(split[2])
city = str(split[3])
if time not in r:
r[time] = {
name: [city]
}
else:
if name not in r[time]:
r[time][name]=[city]
else:
r[time][name].append(city)
for i in transactions:
split = i.decode("utf-8").split(",")
name = str(split[0])
time = int(split[1])
amount = int(split[2])
city = str(split[3])
if amount > 1000:
inv.append(i)
continue
for j in range(time-60, time+61):
if j not in r:
continue
if name not in r[j]:
continue
if len(r[j][name]) > 1 or (r[j][name][0] != city):
inv.append(i)
break
return inv
class transaction {
public:
int time;
int amount;
string city;
string tran;
bool marked;
transaction (int t, int a, string c, string tr) {
tran = tr;
time = t;
amount = a;
city = c;
marked = false;
}
};
class Solution {
public:
vector<string> invalidTransactions(vector<string>& transactions) {
if(transactions.empty())
return {};
unordered_map<string, vector<transaction*>> trans;
for(string &st : transactions) {
istringstream ss(st);
string token = "";
getline(ss, token, ',');
string name = token;
getline(ss, token, ',');
int time = stoi(token);
getline(ss, token, ',');
int amount = stoi(token);
getline(ss, token, ',');
string city = token;
transaction *t = new transaction(time, amount, city, st);
trans[name].push_back(t);
}
vector<string> result;
for(auto &p : trans) {
sort(p.second.begin(), p.second.end(), [](const transaction* a, const transaction* b) {
return a->time < b->time;
});
for(int i=0; i<p.second.size(); i++) {
if(p.second[i]->amount > 1000) {
result.push_back(p.second[i]->tran);
p.second[i]->marked = true;
}
if(i > 0 && p.second[i]->time <= p.second[i-1]->time + 60) {
int r = i-1;
while(r >= 0 && p.second[r]->time >= p.second[i]->time - 60) {
if(p.second[i]->city != p.second[r]->city) {
if(!p.second[r]->marked) {
result.push_back(p.second[r]->tran);
p.second[r]->marked = true;
}
if(!p.second[i]->marked) {
p.second[i]->marked = true;
result.push_back(p.second[i]->tran);
}
}
r--;
}
}
}
}
return result;
}
};
In our experience, we suggest you solve this Invalid Transactions LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Invalid Transactions LeetCode Solution
I hope this Invalid Transactions LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>