LeetCode 1169: Invalid Transactions Solution

Master LeetCode problem 1169 (Invalid Transactions), 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.

1169. Invalid Transactions

Problem Explanation

Explanation

To solve this problem, we can iterate through each transaction and check if it is possibly invalid based on the given conditions. We can use a map to keep track of transactions made by each person in different cities along with their times. Then, for each transaction, we check if the amount exceeds $1000 or if there is a previous transaction within 60 minutes by the same person but in a different city.

Algorithm:

  1. Create a map to store transactions by person name.
  2. Iterate through each transaction and split it into name, time, amount, and city.
  3. Check if the amount exceeds $1000. If so, mark the transaction as invalid.
  4. Check if there is a previous transaction within 60 minutes by the same person but in a different city. If so, mark both transactions as invalid.
  5. Return the list of possibly invalid transactions.

Time complexity: O(n^2) where n is the number of transactions Space complexity: O(n)

Solution Code

import java.util.*;

class Solution {
    public List<String> invalidTransactions(String[] transactions) {
        List<String> result = new ArrayList<>();
        Map<String, List<String>> map = new HashMap<>();

        for (String transaction : transactions) {
            String[] parts = transaction.split(",");
            String name = parts[0];
            int time = Integer.parseInt(parts[1]);
            int amount = Integer.parseInt(parts[2]);
            String city = parts[3];

            if (amount > 1000) {
                result.add(transaction);
            }

            if (!map.containsKey(name)) {
                map.put(name, new ArrayList<>());
            }

            for (String prev : map.get(name)) {
                String[] prevParts = prev.split(",");
                int prevTime = Integer.parseInt(prevParts[1]);
                String prevCity = prevParts[3];

                if (!prevCity.equals(city) && Math.abs(prevTime - time) <= 60) {
                    result.add(transaction);
                    result.add(prev);
                }
            }

            map.get(name).add(transaction);
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1169 (Invalid Transactions)?

This page provides optimized solutions for LeetCode problem 1169 (Invalid Transactions) 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 1169 (Invalid Transactions)?

The time complexity for LeetCode 1169 (Invalid Transactions) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1169 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1169 in Java, C++, or Python.

Back to LeetCode Solutions