Invalid Transactions in C++


Suppose there are some transactions. 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.

Here each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction. We have a list of transactions, find a list of transactions that are possibly invalid. So if the input is like ["alice,20,800,mtv", "bob,50,1200,mtv"], then the answer will be ["bob,50,1200,mtv"].

To solve this, we will follow these steps −

  • Define a set s. Define a map m

  • for i in range 0 to size of t – 1

    • x := t[i]

    • temp := node using string x

    • for j in range 0 to size of m[name of temp]

      • y := m[name of temp][j]

      • if city of y is not city of temp and |time of y – time of temp| −= 60

        • insert node y into set s as string, and insert x into s

    • if the amount of temp > 1000, then insert x into s

    • insert temp into m[name of temp]

  • return the items in the set s

Example(C++)

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Node{
   public:
   string name;
   string city;
   int time;
   int amount;
};
class Solution {
   public:
   Node getNode(string s){
      string temp = "";
      Node ret;
      int cnt = 0;
      for(int i = 0; i < s.size(); i++){
         if(s[i] == ','){
            if(cnt == 0){
               ret.name = temp;
            }
            else if(cnt == 1){
               ret.time = stoi(temp);
            }
            else if(cnt == 2){
               ret.amount = stoi(temp);
            } else {
               ret.city = temp;
            }
            cnt++;
            temp = "";
            continue;
         }
         temp += s[i];
      }
      ret.city = temp;
      return ret;
   }
   vector<string> invalidTransactions(vector<string>& t) {
      set <string >s;
      map <string ,vector < Node >> m;
      for(int i = 0; i < t.size(); i++){
         string x = t[i];
         Node temp = getNode(x);
         for(int j = 0; j < m[temp.name].size(); j++){
            Node y = m[temp.name][j];
            if(y.city != temp.city && abs(y.time - temp.time) <= 60){
               s.insert(y.name + "," + to_string(y.time) + "," + to_string(y.amount) + "," + y.city);
               s.insert(x);
            }
         }
         if(temp.amount > 1000){
            s.insert(x);
         }
         m[temp.name].push_back(temp);
      }
      vector <string> ret(s.begin(), s.end());
      return ret;
   }
};
main(){
   vector<string> v1 = {"alice,20,800,mtv","bob,50,1200,mtv"};
   Solution ob;
   print_vector(ob.invalidTransactions(v1));
}

Input

["alice,20,800,mtv","bob,50,1200,mtv"]

Output

[bob,50,1200,mtv, ]

Updated on: 02-May-2020

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements