中级农民
- 积分
- 195
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-4-6
- 最后登录
- 1970-1-1
|
面筋中常见非LC题目的代码。解法是根据之前帖子的讨论和自己的理解:
Buffered File
# 注意这个buffer是达到buffered threshold之后一次性清空所有buffer写入硬盘的情况
class BufferedFile:
def __init__(self, maxBufferedSize):
self.maxBufferedSize=maxBufferedSize
self.diskStorage=[]
self.buffer=[]
def write(self, content):
for ch in content:
self.buffer.append(ch)
if len(self.buffer)==self.maxBufferedSize:
self.flush()
#面试的时候会给你专门的dummy writing to hard disc API,只要调用就好
def flush(self):
self.diskStorage.extend(self.buffer)
self.buffer = []
CoinCounts 的各个种类coins分别的个数
# 给你一个数组的硬币面值种类,求组合一个target面额的最少硬币个数方案下各个种类硬币数。
# 如果给你的硬币种类最小单位是1,可以用贪心算法。但是如果没有1,那贪心算法未必可行,必须用heuristic DFS,即用贪心算法回溯性的尝试。
class CoinCounts:
def getCoins(self, coinTypes, value):
def helper(target, coinIdx):
if target==0:
return [0]*len(coinTypes)
if target<0:
return None
if coinIdx<0:
return None
choices = target//coinTypes[coinIdx]
for cnt in range(choices, -1, -1):
rest = helper(target-coinTypes[coinIdx]*cnt, coinIdx-1)
if rest is not None:
rest[coinIdx] += cnt
return rest
return None
res= helper(value, len(coinTypes)-1)
if res:
return res
else:
return []
delete directories
class FileSystem:
def __init__(self):
pass
def findList(self,path):
pass
def delete(self, path):
pass
def isDir(self, path):
pass
fs = FileSystem()
def deleteDirs(path, fs):
if isDir(path):
children = fs.findList(path)
for child in children:
deleteDirs(child, fs)
fs.delete(path)
high performance filter
有一个数据流会进来一些tags比如
['apple, facebook, google', 'banana, facebook', 'facebook, google, tesla', 'intuit, google,
facebook']
然后有一个filter list, 根据filter list输出这些Tags的补集
比如filter by ['apple']那么return ['facebook', 'google'] (只有第一个里面有APPLE)
比如filter by ['facebook', 'google']那么return ['apple', 'tesla','intuit']
有人给出用reverted index。我就按照这个思路给一个解法:
class HighPerformanceFilter:
def __init__(self):
self.tagsList = {}
self.revertedIdx = defaultdict(list)
self.id=1
def addTags(self, tagsStr):
tags = list(map(lambda x: x.strip(), tagsStr.split(",")))
self.tagsList[self.id]=tags
for t in tags:
self.revertedIdx[t].append(self.id)
self.id+=1
def filter(self, inputTags):
res = []
idxGroups = defaultdict(list)
for tag in inputTags:
if tag in self.revertedIdx:
for tid in self.revertedIdx[tag]:
idxGroups[tid].append(tag)
for idx in idxGroups:
targetSet = set(self.tagsList[idx])
allIncluded=True
for ctag in inputTags:
if ctag in targetSet:
targetSet.remove(ctag)
else:
allIncluded=False
break
if allIncluded:
res.extend(targetSet)
return res
Flights Vacations
# 地里汇报这题没有LC中的城市间flights约束,默认所有城市都有航班,所以比较简单。但是有一个follow up: 要求总flights最少。这哥么说的很简单,没有继续解释。所以我只能猜测他说的是指还是要求vacation days最多,但是如果有两个方案vacation days一样多,那就选需要flights少的那个方案,并返回总vacation days 和 相对应的flights counts。 以下代码基于这个假设。
class Solution:
def maxVacationDays2(self, days: list[list[int]]) -> (int, int):
# dp{week}[cityIdx]=(vday, flight) compare: (vday1, flight1) (vday2, flight2) if vday1>vday2: 1
# if vday1<vday2: 2 if vday1==vday2 flight1>=flight2? 1:2
nweek=len(days)
mcities=len(days[0])
thisWeekDp=[]
if nweek<1:
return 0
for i in range(mcities):
vday = days[0][i]
if i==0:
thisWeekDp.append((vday, 0))
else:
thisWeekDp.append((vday, 1))
for i in range(1, nweek):
ntWeekDp=thisWeekDp.copy()
for j in range(0, mcities):
for k in range(0, mcities):
if thisWeekDp[k][0]+days[i][j]>ntWeekDp[j][0]:
if j==k:
ntWeekDp[j]=(thisWeekDp[k][0]+days[i][j], thisWeekDp[k][1])
else:
ntWeekDp[j] = (thisWeekDp[k][0] + days[i][j], thisWeekDp[k][1]+1)
elif thisWeekDp[k][0]+days[i][j]==ntWeekDp[j][0]:
if j==k:
if thisWeekDp[k][1]<ntWeekDp[j][1]:
ntWeekDp[j][1]=thisWeekDp[k][1]
else:
if thisWeekDp[k][1]+1<ntWeekDp[j][1]:
ntWeekDp[j][1] = thisWeekDp[k][1]+1
thisWeekDp=ntWeekDp
best=(0, 0)
for choice in thisWeekDp:
if choice[0]>best[0]:
best=choice
elif choice[0]==best[0]:
if choice[1]<best[1]:
best=choice
return best |
|