【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 between1
and10
.- Each
{time}
consist of digits, and represent an integer between0
and1000
.- Each
{amount}
consist of digits, and represent an integer between0
and2000
.
解题思路:题目不难,但是坑多。我的方法是以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 ...
随机推荐
- Python Module_Socket_网络编程
目录 目录 Socket 套接字 套接字的原理 套接字的数据处理方式 套接字类型 Socket 标准函数 ServerSocket 标准函数 ClientSocket 标准函数 公有标准函数 Sock ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_6 mybatis注解开发一对一的查询配置
新建Account实体类 生成getter和setter还有toString方法 先创建dao类 全局的配置,这里要改成package 创建多对一的关系 在查询的时候输出user这个对象的内容 建立查 ...
- 阶段3 1.Mybatis_03.自定义Mybatis框架_4.自定义mybatis的编码-解析XML的工具类介绍
导入xml操作的类和用到的相关包 创建util包,然后把提供好的XMLConfigBuilder.java文件复制3过来 复制过来,里面用到了很多dom4j的东西 打开pom.xml 输入depend ...
- TField中的GetText和SetText
在数据表中的某些字段出于性能或数据规范化的考虑,会用组编号代替,就像学生有学号,员工有员工ID一样,但我们看的时候如果直接输入这样的编号看的人可能就会头痛了,这时就可用TField中的GetText转 ...
- SpringContextHolder使用报错
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dobecoder/article/details/88401612今天在使用SpringContex ...
- 【转载】linux下如何使用sftp命令
原文地址:http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个交互式文件传输程式.它类似于 ftp, ...
- Eclipse连接SQL Server 2008数据库
一.准备材料 要能够使用数据库就要有相应的JDBC,所以我们要去Microsoft官网下载 https://www.microsoft.com/zh-cn/download/details.aspx? ...
- webapi接口统一返回请求时间
webapi接口统一返回请求时间: public class BaseController : ControllerBase { protected ReturnResult<T> Res ...
- mysql 大数据分页优化
一.mysql大数据量使用limit分页,随着页码的增大,查询效率越低下. 1. 直接用limit start, count分页语句, 也是我程序中用的方法: select * from prod ...
- PythonDay07
第七章 今日内容 基础数据类型补充 以后会遇到的坑 二次编码 基础类型补充 stra = "One two"print(a.capitalize()) # 首字母大写print ...