Invalid Transactions in C++

C++Server Side ProgrammingProgramming

<p>Suppose there are some transactions. A transaction is possibly invalid if &minus;</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 [&quot;alice,20,800,mtv&quot;, &quot;bob,50,1200,mtv&quot;], then the answer will be [&quot;bob,50,1200,mtv&quot;].</p><p>To solve this, we will follow these steps &minus;</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 &ndash; 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 &ndash; time of temp| &minus;= 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 &gt; 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 &minus;</p><p><a class="demo" href="http://tpcg.io/6PaiHPgs" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">#include &lt;bits/stdc++.h&gt; using namespace std; void print_vector(vector&lt;auto&gt; v){ &nbsp; &nbsp;cout &lt;&lt; &quot;[&quot;; &nbsp; &nbsp;for(int i = 0; i&lt;v.size(); i++){ &nbsp; &nbsp; &nbsp; cout &lt;&lt; v[i] &lt;&lt; &quot;, &quot;; &nbsp; &nbsp;} &nbsp; &nbsp;cout &lt;&lt; &quot;]&quot;&lt;&lt;endl; } class Node{ &nbsp; &nbsp;public: &nbsp; &nbsp;string name; &nbsp; &nbsp;string city; &nbsp; &nbsp;int time; &nbsp; &nbsp;int amount; }; class Solution { &nbsp; &nbsp;public: &nbsp; &nbsp;Node getNode(string s){ &nbsp; &nbsp; &nbsp; string temp = &quot;&quot;; &nbsp; &nbsp; &nbsp; Node ret; &nbsp; &nbsp; &nbsp; int cnt = 0; &nbsp; &nbsp; &nbsp; for(int i = 0; i &lt; s.size(); i++){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(s[i] == &#39;,&#39;){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(cnt == 0){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret.name = temp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(cnt == 1){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret.time = stoi(temp); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(cnt == 2){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret.amount = stoi(temp); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret.city = temp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = &quot;&quot;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp += s[i]; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; ret.city = temp; &nbsp; &nbsp; &nbsp; return ret; &nbsp; &nbsp;} &nbsp; &nbsp;vector&lt;string&gt; invalidTransactions(vector&lt;string&gt;&amp; t) { &nbsp; &nbsp; &nbsp; set &lt;string &gt;s; &nbsp; &nbsp; &nbsp; map &lt;string ,vector &lt; Node &gt;&gt; m; &nbsp; &nbsp; &nbsp; for(int i = 0; i &lt; t.size(); i++){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string x = t[i]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Node temp = getNode(x); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int j = 0; j &lt; m[temp.name].size(); j++){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node y = m[temp.name][j]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(y.city != temp.city &amp;&amp; abs(y.time - temp.time) &lt;= 60){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.insert(y.name + &quot;,&quot; + to_string(y.time) + &quot;,&quot; + to_string(y.amount) + &quot;,&quot; + y.city); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.insert(x); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(temp.amount &gt; 1000){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.insert(x); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;m[temp.name].push_back(temp); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; vector &lt;string&gt; ret(s.begin(), s.end()); &nbsp; &nbsp; &nbsp; return ret; &nbsp; &nbsp;} }; main(){ &nbsp; &nbsp;vector&lt;string&gt; v1 = {&quot;alice,20,800,mtv&quot;,&quot;bob,50,1200,mtv&quot;}; &nbsp; &nbsp;Solution ob; &nbsp; &nbsp;print_vector(ob.invalidTransactions(v1)); }</pre><h2>Input</h2><pre class="result notranslate">[&quot;alice,20,800,mtv&quot;,&quot;bob,50,1200,mtv&quot;]</pre><h2>Output</h2><pre class="result notranslate">[bob,50,1200,mtv, ]</pre>
raja
Updated on 02-May-2020 09:25:30

Advertisements