

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Invalid Transactions in C++
<p>Suppose there are some transactions. A transaction is possibly invalid if −</p><ul class="list"><li><p>The amount exceeds $1000, or;</p></li><li><p>If it occurs within (and including) 60 minutes of another transaction with the same name in a different city.</p></li></ul><p>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"].</p><p>To solve this, we will follow these steps −</p><ul class="list"><li><p>Define a set s. Define a map m</p></li><li><p>for i in range 0 to size of t – 1</p><ul class="list"><li><p>x := t[i]</p></li><li><p>temp := node using string x</p></li><li><p>for j in range 0 to size of m[name of temp]</p><ul class="list"><li><p>y := m[name of temp][j]</p></li><li><p>if city of y is not city of temp and |time of y – time of temp| −= 60</p><ul class="list"><li><p>insert node y into set s as string, and insert x into s</p></li></ul></li></ul></li><li><p>if the amount of temp > 1000, then insert x into s</p></li><li><p>insert temp into m[name of temp]</p></li></ul></li><li><p>return the items in the set s</p></li></ul><h2>Example(C++)</h2><p>Let us see the following implementation to get a better understanding −</p><p><a class="demo" href="http://tpcg.io/6PaiHPgs" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate" style="">#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)); }</pre><h2>Input</h2><pre class="result notranslate">["alice,20,800,mtv","bob,50,1200,mtv"]</pre><h2>Output</h2><pre class="result notranslate">[bob,50,1200,mtv, ]</pre>
- Related Questions & Answers
- Remove Invalid Parentheses in C++
- Explain about concurrent transactions in DBMS
- Performing Database Transactions using Python
- Role of CSS :invalid Selector
- Handling failed transactions in SAP HANA system
- Invalid Connection String in SAP Business One
- How can we use nested transactions in MySQL?
- How are the transactions recorded in respective journals?
- Automating SAP Transactions/actions using SAP GUI
- Safety Tips for Making Secure Mobile Transactions
- How can we use nested transactions allowed in MySQL?
- Difference between SE01, SE09 and SE10 Transactions in SAP
- How transactions are entered in a petty cash book?
- C++ Remove Invalid Parentheses from an Expression
- How to handle invalid arguments with argparse in Python?
Advertisements