1169. Invalid Transactions**

1169. Invalid 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.

Each transaction string​​ ​transactions[i]​​ consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.

Given a list of ​ ​transactions​​, return a list of transactions that are possibly invalid. You may return the answer in any order.

Example 1:

Example 2:

Example 3:

Constraints:

  • &#x200B;transactions.length <= 1000​< code>&#x200B;<!--=-->
  • Each​ &#x200B;transactions[i]&#x200B;​​ takes the form​ &#x200B;"{name},{time},{amount},{city}"&#x200B;
  • Each​ &#x200B;{name}&#x200B;​​ and​ &#x200B;{city}&#x200B;​​ consist of lowercase English letters, and have lengths between​ &#x200B;1&#x200B;​​ and​ &#x200B;10&#x200B;​.

  • Each​ &#x200B;{time}&#x200B;​​ consist of digits, and represent an integer between​ &#x200B;0&#x200B;​​ and​ &#x200B;1000&#x200B;​​.

Each​​ &#x200B;{amount}&#x200B;​​ consist of digits, and represent an integer between​ &#x200B;0&#x200B;​​ and​ &#x200B;2000&#x200B;​.

C++ 实现 1

此题差评, 描述不清, 另外我看题也不太认真, 浪费了大量时间. ????????????

读懂题意是关键. 这道题要求最后返回的是 invalid 的交易, 而交易被满足如下条件之一就被定义为 invalid:

  • 本次交易的​ &#x200B;amount > 1000&#x200B;
  • 在本次交易 A 的股票名字和另一次交易 B 的股票名字相同的情况下, 交易发生的地点(city)不同并且交易时间在​ &#x200B;60&#x200B;​ 分钟之内.

第二点限制条件可以理解为不能在短时间内跑到另一个城市对同一只股票发生交易.

然而, 要真正理解两个限制条件并写出代码, 我们有一些更深层的问题要问, 这可以从题目给出的示例出发. 第 1 个例子中, 由于不满足第二个条件, 所以两个交易都是 invalid 的. 第 2 个例子, 需要特别注意, 这个例子之所以返回 ​ &#x200B;["alice,50,1200,mtv"]&#x200B;​​ 这个结果, 一方面我们考虑到第一个条件的限制, 而另一方面, 第二个条件也会对结果产生影响, 原因是输入的两个交易 ​ &#x200B;["alice,20,800,mtv","alice,50,1200,mtv"]&#x200B;​​ 它们发生的交易地点都是 ​ &#x200B;"mtv"&#x200B;​​, 在同一个城市, 所以即使时间上的差值小于 60, ​ &#x200B;"alice,20,800,mtv"&#x200B;​​ 这个交易仍然是有效的. 假设这个示例的输入是 ​ &#x200B;["alice,20,800,omg","alice,50,1200,mtv"]&#x200B;​, 此时返回什么 ? !
此时返回 ​​ &#x200B;["alice,20,800,omg","alice,50,1200,mtv"]&#x200B;​​, 原因它们不满足第二个条件, (额外的 ​ &#x200B;"alice,50,1200,mtv"&#x200B;​ 还不满足第一个条件).

下面代码的逻辑是:

  • &#x200B;parse_transaction&#x200B;​​ 函数中, 使用​ &#x200B;record&#x200B;​​ 保存每个访问过的 transaction,​ &#x200B;res&#x200B;​ 保存 invalid 的 transaction

  • &#x200B;record&#x200B;​​ 的结构比较复杂, 表示为​ &#x200B;record[name][city].push_back({time, amount})&#x200B;​​, 第一个 map 的 key 是股票的 name, 而​ &#x200B;record[name]&#x200B;​​ 中的 map 的 key 对应的是 city, 而​ &#x200B;record[name][city]&#x200B;​​ 对应的 vector 保存的是​ &#x200B;{time, amount}&#x200B;​ pair.

  • 对于当前访问的 transaction, 我们看​ &#x200B;name&#x200B;​​ 是否已经存在于​ &#x200B;record&#x200B;​​ 中. 不存在就放心的加入到​ &#x200B;record&#x200B;​​ 中, 因为股票名字​ &#x200B;name&#x200B;​ 不同, 不会影响 invalid transaction 的判断.

  • 如果​ &#x200B;record&#x200B;​​ 已经存在了相同的​ &#x200B;name&#x200B;​​, 也就是说, 针对​ &#x200B;name&#x200B;​ 这只股票, 应该有交易发生了. 我们这时候就要判断它是否会违反第二个约束条件. 注意, 这里暂时不考虑第一个约束条件, 因为不管 amount 有没有超过 1000, 该交易都是要存储到 record 中! 这一点需要切记.

  • 遍历​ &#x200B;name&#x200B;​​ 这只股票发生交易的所有城市​ &#x200B;for (auto &city_record : record[name])&#x200B;​, 只有当城市不同的时候, 才需要进行时间上的比较.

  • 使用​ &#x200B;invalid&#x200B;​​ 这个 bool 值来判断是否需要将本次交易设置为 invalid 的. 如果上一步的时间比较发现, 存在交易 B 和本次交易 A 发生的地点不同, 但是时间相差不超过 60, 那么不仅需要将交易 B 存入 invalid 交易集合​ &#x200B;res&#x200B;​​ 中, 当比较完所有的交易后, 还要记得将本次交易 A 存入​ &#x200B;res&#x200B;​ 中.

  • 之后, 如果​ &#x200B;invalid == true&#x200B;​​ 或者本次交易的​ &#x200B;amount > 1000&#x200B;​​, 那么就将本次交易加入​ &#x200B;res&#x200B;​ 中.

  • 最后注意, 访问的每次交易都要存入​ &#x200B;record&#x200B;​ 中!

Original: https://blog.51cto.com/u_15661962/5343565
Author: 珍妮的选择
Title: 1169. Invalid Transactions**

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/517688/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球