LeetCode 1843: Suspicious Bank Accounts Solution
Master LeetCode problem 1843 (Suspicious Bank Accounts), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1843. Suspicious Bank Accounts
Problem Explanation
Explanation:
The problem involves identifying suspicious bank accounts based on certain criteria. Given a list of transactions where each transaction contains the unique transaction id, the user name, the amount, and the city, we need to find the suspicious accounts. An account is considered suspicious if there are at least three different users that have transferred money to or received money from the account within a single day.
To solve this problem, we can follow these steps:
- Create a HashMap to store the transactions grouped by account id.
- For each account, sort the transactions by the timestamp and check if there are at least three different users involved in the transactions within a day.
- Mark the suspicious accounts and return the list of suspicious account ids.
Time complexity: O(n log n) where n is the number of transactions. Space complexity: O(n) where n is the number of transactions.
:
Solution Code
class Solution {
public List<String> suspiciousActivity(List<List<String>> transactions) {
Map<String, List<List<String>>> map = new HashMap<>();
List<String> suspiciousAccounts = new ArrayList<>();
for (List<String> transaction : transactions) {
String accountId = transaction.get(0);
String userName = transaction.get(1);
int amount = Integer.parseInt(transaction.get(2));
String city = transaction.get(3);
map.putIfAbsent(accountId, new ArrayList<>());
map.get(accountId).add(Arrays.asList(userName, String.valueOf(amount), city));
}
for (String accountId : map.keySet()) {
List<List<String>> accountTransactions = map.get(accountId);
Collections.sort(accountTransactions, (a, b) -> Integer.compare(Integer.valueOf(a.get(1)), Integer.valueOf(b.get(1)));
Set<String> users = new HashSet<>();
for (List<String> transaction : accountTransactions) {
String userName = transaction.get(0);
if (users.contains(userName)) {
suspiciousAccounts.add(accountId);
break;
}
users.add(userName);
}
}
return suspiciousAccounts;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1843 (Suspicious Bank Accounts)?
This page provides optimized solutions for LeetCode problem 1843 (Suspicious Bank Accounts) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1843 (Suspicious Bank Accounts)?
The time complexity for LeetCode 1843 (Suspicious Bank Accounts) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1843 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1843 in Java, C++, or Python.