Advertisement

Invalid Transactions - LeetCode 1169 Solution

Invalid Transactions - Complete Solution Guide

Invalid Transactions is LeetCode problem 1169, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A transaction is possibly invalid if: the amount exceeds $1000 , or; if it occurs within (and including) 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

Detailed Explanation

The problem asks us to identify potentially invalid transactions from a list of transactions. Each transaction contains the name of the person, the time of the transaction, the amount, and the city where the transaction occurred. A transaction is considered invalid if either its amount exceeds $1000, or if it happens within 60 minutes of another transaction by the same person in a different city. The input is a list of strings, each string representing a transaction. The output should be a list of strings representing the invalid transactions, in any order.

Solution Approach

The solution involves iterating through the list of transactions. For each transaction, it first checks if the amount is greater than 1000. If it is, the transaction is marked as invalid. Then, it iterates through the rest of the transactions, comparing each transaction with the current one. If another transaction has the same name and a different city, and occurs within 60 minutes of the current transaction, then both the current transaction and the other transaction are marked as invalid. Finally, a list of invalid transactions is returned.

Step-by-Step Algorithm

  1. Step 1: Parse each transaction string into its individual components: name, time, amount, and city.
  2. Step 2: Iterate through the parsed transactions.
  3. Step 3: For each transaction, check if the amount exceeds $1000. If it does, mark the transaction as invalid.
  4. Step 4: Iterate through the remaining transactions.
  5. Step 5: Compare the current transaction with each of the other transactions. If the name is the same, the city is different, and the time difference is within 60 minutes (inclusive), mark both transactions as invalid.
  6. Step 6: Collect all the transactions marked as invalid into a result list.
  7. Step 7: Return the list of invalid transactions.

Key Insights

  • Insight 1: Parsing the transaction string into its components (name, time, amount, city) is crucial.
  • Insight 2: We need to compare each transaction against all other transactions made by the same person to check for proximity in time and difference in city.
  • Insight 3: Using a hash table or map to group transactions by name can optimize the comparison process.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, String, Sorting.

Companies

Asked at: PayPal, Wix.