LeetCode每日一题(1169. Invalid Transactions)

这篇博客主要介绍了如何识别可能无效的交易。通过对输入的交易数组进行排序和遍历,检查每笔交易是否在60分钟内与另一笔同名且位于不同城市的交易发生,或者金额超过1000。如果满足这些条件,交易则被认为是无效的。博客提供了具体的实现代码,展示了如何处理这种情况。

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,20,800,mtv”,“alice,50,100,beijing”]
Output: [“alice,20,800,mtv”,“alice,50,100,beijing”]

Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.

Example 2:

Input: transactions = [“alice,20,800,mtv”,“alice,50,1200,mtv”]
Output: [“alice,50,1200,mtv”]

Example 3:

Input: transactions = [“alice,20,800,mtv”,“bob,50,1200,mtv”]
Output: [“bob,50,1200,mtv”]

Constraints:

  • transactions.length <= 1000
  • Each transactions[i] takes the form “{name},{time},{amount},{city}”
  • Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
  • Each {time} consist of digits, and represent an integer between 0 and 1000.
  • Each {amount} consist of digits, and represent an integer between 0 and 2000.

先将 transactions 按 name, time 进行排序, 然后新建一个 stack 用以维护已检查过的 transaction, 遍历 transactions, 对于每个 transactions[i], 我们反向遍历 stack 查找是否有 stack[j].name == trasactions[i].name && transactions[i].time - stack[j].time <= 60 && stack[j].city != transactions[i].city 的 transaction 存在, 如果存在,证明 transactions[i]与 stack[j]都是 invalid. 如果该检查通过,我们还需要检查 amount < 1000。 注意,因为 invalid transaction 也是 transaction, 所以也需要压入 stack, 我们只需要添加一个标识来标识该 transaction 是不是 invalid。最后我们将 stack 中所有 invalid 的 transaction 都取出来就可以了


impl Solution {
    pub fn invalid_transactions(transactions: Vec<String>) -> Vec<String> {
        let mut trans: Vec<(String, i32, String, i32)> = transactions
            .into_iter()
            .map(|s| {
                let l: Vec<String> = s.split(",").map(str::to_owned).collect();
                (
                    l[0].clone(),
                    l[1].parse::<i32>().unwrap(),
                    l[3].clone(),
                    l[2].parse::<i32>().unwrap(),
                )
            })
            .collect();
        trans.sort();
        let mut stack = Vec::new();
        for (name, time, city, amount) in trans {
            if stack.is_empty() {
                stack.push((name, time, city, amount, amount < 1000));
                continue;
            }
            let mut i = stack.len() as i32 - 1;
            let mut got = false;
            while i >= 0 {
                let (p_name, p_time, p_city, _, is_ok) = &mut stack[i as usize];
                if p_name != &name || time - *p_time > 60 {
                    break;
                }
                if p_name == &name && time - *p_time <= 60 && p_city != &city {
                    *is_ok = false;
                    got = true;
                }
                i -= 1;
            }
            stack.push((name, time, city, amount, !got && amount < 1000));
        }
        stack
            .into_iter()
            .filter(|(_, _, _, _, is_ok)| is_ok == &false)
            .map(|(name, time, city, amount, _)| format!("{},{},{},{}", name, time, amount, city))
            .collect()
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值