-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |