【leetcode】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:
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 between1and10.- Each
{time}consist of digits, and represent an integer between0and1000.- Each
{amount}consist of digits, and represent an integer between0and2000.
解题思路:题目不难,但是坑多。我的方法是以name为key值,把每个人的交易记录存入字典中,然后遍历每个人的交易记录。每找到一个不合法的记录,再用这个记录和这个人其他所有的记录比较,直到找出所有符合条件的结果为止。
代码如下:
class Solution(object):
def invalidTransactions(self, transactions):
"""
:type transactions: List[str]
:rtype: List[str]
"""
def cmpf(v1,v2):
lv1 = v1.split(',')
lv2 = v2.split(',')
return int(lv1[1]) - int(lv2[1])
transactions.sort(cmp = cmpf) res = [] dic = {} dic_invalid = {} for transaction in transactions:
if transaction == 'lee,158,987,mexico':
pass
n, t, a, c = transaction.split(",")
if int(a) > 1000:
if transaction not in dic_invalid:
res.append(transaction)
dic_invalid[transaction] = 1
if n in dic:
for hn,ht,ha,hc in dic[n]:
if hc != c and (int(t) - int(ht)) <= 60:
tran = hn + ',' + ht + ',' + ha + ',' + hc
if tran not in dic_invalid:
dic_invalid[tran] = 1
res.append(tran)
if transaction not in dic_invalid:
res.append(transaction)
dic_invalid[transaction] = 1
dic[n] = dic.setdefault(n,[]) + [(n,t,a,c)]
return res
【leetcode】1169. Invalid Transactions的更多相关文章
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- OpenStack Rally 质量评估与自动化测试利器
目录 文章目录 目录 问题描述 Rally 简介 应用场景 应用案例 Rally 安装 Rally 使用 Rally 架构 Rally Plugin 分析与实现 程序入口 执行 rally task ...
- jmeter函数介绍(1)
1._Random() 随机数 The minimum value allowed for a range of values:输入一个最小值: The maximum value allowed f ...
- C++笔记(7)——一些模拟题:简单模拟、查找元素、图形输出、日期处理、进制转换、字符串处理
以下内容基本来自<算法笔记>,作者为胡凡,建议直接买书看,我这里只是摘抄部分当笔记,不完整的. 简单模拟 就是一类"题目怎么说你就怎么做"的题目.这类题目不涉及算法,只 ...
- zimg 服务器配置文件
--zimg server config --server config --是否后台运行 is_daemon = --绑定IP ip = '0.0.0.0' --端口 port = --运行线程数, ...
- 20191112 Spring Boot官方文档学习(4.3)
4.3.Profiles Spring Profiles提供了一种隔离部分应用程序配置并使之仅在某些环境中可用的方法.任何@Component,@Configuration或@Configuratio ...
- 关于java范型
1 范型只在编译阶段有效 编译器在编译阶段检查范型结果之后,就会将范型信息删除.范型信息不会进入运行时阶段. 泛型类型在逻辑上看以看成是多个不同的类型,实际上都是相同的基本类型. 2 不能对确定的范型 ...
- go net库
1 使用Listen函数创建一个server ln, err := net.Listen("tcp", ":8080") if err != nil { // ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- 服务性能指标:PV、UV、TPS、QPS
名词解释 PV Page View,网页浏览量.网页被读者调用浏览的次数.网页每次打开或刷新一次页面,记录一次.用户对同一页面的多次访问,访问量累计. UV Unique Visitor,独立访问者. ...
- IDEA闪退问题
这段时间经常遇到IDEA闪退的问题,在网上搜了一大堆的博客,无外乎是说让修改下面两个文件,但是改来改去没什么卵用,最后重装IDEA,一样的,没什么用.持续时间有几个月了,内心也有点崩溃,昨天下午彻底心 ...