Skip to content

Commit

Permalink
2019-08-25
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Aug 26, 2019
1 parent 2f12988 commit cad5291
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 1169.查询无效交易/1169-查询无效交易.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution(object):
def invalidTransactions(self, transactions):
"""
:type transactions: List[str]
:rtype: List[str]
"""
recordByName = collections.defaultdict(list)
for trans in transactions:
name, time, amount, city = trans.split(",")
recordByName[name].append([name, int(time), int(amount), city])

def convert(l):
return l[0] + "," + str(l[1]) + "," + str(l[2]) + "," + l[3]

res = set()
for name, rec in recordByName.items():
curRec = sorted(rec, key = lambda x:x[1])

for i in range(len(curRec)):
if curRec[i][2] > 1000:
res.add(convert(curRec[i]))
for j in range(i + 1, len(curRec)):

if abs(curRec[j][1] - curRec[i][1]) > 60:
break
if curRec[j][3] != curRec[i][3]:
res.add(convert(curRec[i]))
res.add(convert(curRec[j]))
return res



Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution(object):
def numSmallerByFrequency(self, queries, words):
"""
:type queries: List[str]
:type words: List[str]
:rtype: List[int]
"""

def func(word):
for char in "abcdefghijklmnopqrstuvwxyz":
if char in word:
return word.count(char)
return 0

def func2(word):
record = collections.Counter(word)
return record[min(record.keys())]


words_count = sorted(map(func2, words))
queries_count = map(func2, queries)
# print words_count, queries_count
ans = []
for query in queries_count:
index = bisect.bisect(words_count, query) #bisect可以迅速找出有index个数 <= query
ans.append(len(words_count) - index)# 减法找有多少个数比query大
return ans

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def removeZeroSumSublists(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy = ListNode(-1)
dummy.next = head

record = {0:dummy}
pre_sum = 0

while head:
pre_sum += head.val
if pre_sum in record:
record[pre_sum].next = head.next
else:
record[pre_sum] = head
head = head.next
return dummy.next
58 changes: 58 additions & 0 deletions 1172.餐盘栈/1172-餐盘栈.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from heapq import *
class DinnerPlates(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.stack = []
self.c = capacity
self.idx = [] #用于存放有空位的栈的下标

def push(self, val):
"""
:type val: int
:rtype: None
"""
if self.idx:
index = heappop(self.idx) #找最小的空的栈的下标
self.stack[index].append(val) #插入val
if len(self.stack[index]) < self.c: #如果插了之后还没满,就把这个空的栈的下标放进self.idx
heappush(self.idx, index)
else: #现在所有栈都满了,只能新增一个栈在末尾
self.stack.append([val])
if self.c > 1:
self.idx.append(len(self.stack) - 1)


def pop(self):
"""
:rtype: int
"""
while self.stack and not self.stack[-1]:
self.stack.pop()
if not self.stack: #所有的栈都是空的
return -1
else:
if len(self.stack[-1]) == self.c: #如果本来是满的栈
heappush(self.idx, len(self.stack) - 1) #现在要变成有空位的栈了
return self.stack[-1].pop()

def popAtStack(self, index):
"""
:type index: int
:rtype: int
"""
if index >= len(self.stack): #下标越界
return -1
else:
s = self.stack[index] # 把下标为index的栈取出来
if len(s) == self.c: #如果这个栈本来是满的
heappush(self.idx, index) #马上要变空了
return s.pop() if s else -1 #如果这个栈是空的,返回-1, 否则返回pop


# Your DinnerPlates object will be instantiated and called as such:
# obj = DinnerPlates(capacity)
# obj.push(val)
# param_2 = obj.pop()
# param_3 = obj.popAtStack(index)

0 comments on commit cad5291

Please sign in to comment.