Similar Problems
Similar Problems not available
Invalid Transactions - Leetcode Solution
LeetCode: Invalid Transactions Leetcode Solution
Difficulty: Medium
Topics: string hash-table sorting array
The Invalid Transactions problem on LeetCode is a problem that requires us to identify invalid transactions based on certain criteria given in the input. The problem statement requires us to find all transactions that meet the following criteria:
- The amount of the transaction exceeds $1000.
- The same name appears in two different transactions, and the time difference between the transactions is less than or equal to 60 minutes.
The input to the problem is given in the form of a comma-separated list of transactions, where each transaction is in the format "name,time,amount,city". Our task is to identify the invalid transactions and return a list of these transactions.
To solve this problem, we can follow the following approach:
- Parse the input and create a list of transactions, where each transaction is represented as a dictionary with keys "name", "time", "amount", "city".
- Create a list of invalid transactions and initialize it to an empty list.
- Iterate over the list of transactions and for each transaction, check if it satisfies any of the two criteria mentioned above.
- If a transaction satisfies criteria one, add it to the list of invalid transactions.
- If a transaction satisfies criteria two, iterate over all previous transactions with the same name and check if the time difference between the current transaction and the previous transaction is less than or equal to 60 minutes. If it is, add both transactions to the list of invalid transactions.
- Return the list of invalid transactions.
Here is the Python code that implements the above approach:
def invalidTransactions(transactions):
# Parse the input and create a list of transactions
transactions_list = []
for transaction in transactions:
name, time, amount, city = transaction.split(",")
transactions_list.append({
"name": name,
"time": int(time),
"amount": int(amount),
"city": city
})
# Create a list of invalid transactions and initialize it to an empty list
invalid_transactions = []
# Iterate over the list of transactions
for i, current_transaction in enumerate(transactions_list):
# Check if the amount of the transaction exceeds $1000
if current_transaction["amount"] > 1000:
invalid_transactions.append(
transactions[i]
)
# Check if the same name appears in two different transactions,
# and the time difference between the transactions is less than or equal to 60 minutes
for j in range(i):
previous_transaction = transactions_list[j]
if (current_transaction["name"] == previous_transaction["name"] and
current_transaction["city"] != previous_transaction["city"] and
abs(current_transaction["time"] - previous_transaction["time"]) <= 60):
invalid_transactions.extend(
[transactions[i], transactions[j]]
)
# Return the list of invalid transactions
return invalid_transactions
This code has a time complexity of O(n^2), where n is the number of transactions. However, the constraints given in the problem statement specify that there can be at most 1000 transactions, so this solution should be efficient enough.
Invalid Transactions Solution Code
1