【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 ...
随机推荐
- 数据科学家人才危机现象,是FOMO还是Silver?
数据科学家人才危机现象,是FOMO还是Silver? 数据科学家的人才短缺和薪水高涨已经达到了顶板,未来还会持续下去吗? 在过去几年中,高级分析(#大数据#分析)空间一直经历着严重的FOMO(害怕错过 ...
- 【MM系列】SAP MM模块-如何修改物料的移动平均价
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-如何修改物料的移动 ...
- c#字符串代码,动态创建编译器
https://www.cnblogs.com/mrma/p/3998679.html 试了,确实可行,在unity也能用 值得注意的是UnityScript.Scripting.Evaluator ...
- note.js 引用jQuery,
1.安装jQuery npm install jquery 在自己的项目目录里面打开cmd进行安装 2.下面直接在js文件里面引用jQuery //调用jQuery来操作页面 const jsdom ...
- 判断RecyclerView是否滚动到底部
转:http://www.jianshu.com/p/c138055af5d2 一.首先,我们来介绍和分析一下第一种方法,也是网上最多人用的方法: public static boolean isVi ...
- python之optparse
Python有两个内建的模块用来处理命令行参数 一个是getopt只能简单处理命令行参数 一个是optparse,功能更强大,而且易于使用,可以方便地生成标准的,符合Unix/Posix规范的命令行说 ...
- Spark启动流程(Standalone)-分析
1.start-all.sh脚本,实际上执行java -cp Master 和 java -cp Worker 2.Master 启动时首先穿件一个RpcEnv对象,负责管理所有通信逻辑 3.Mast ...
- Linux 创建与删除(5)
相对于Windows下的右键新建文件与删除,我更喜爱Linux下的命令式创建与删除,真的方便.不过Windows下也可以借助工具来实现,比如git bash.cmder等等终端工具. 创建文件 新建文 ...
- linux下的变量规则
1.变量名可以由字母.数字和下划线组成,但是不能以数字开头: 2.变量在命名时,默认情况下为字符型: 3.为变量赋值时,等号两边不能有空格: 4.如果要增加变量的值,可以进行变量的叠加,不过需要将变量 ...
- Ubantu上安装Redis
Ubantu上安装Redis:Redis(Remote Dictionary Server):远程字典服务器,简称REDIS;Redis数据库产品用C语言编写而成,开源.少量数据存储.高速读写访问,是 ...