r**h 发帖数: 1288 | 1 lst = [('a',2), ('b',3), ('a',34), ('a',3), ('b',8), ('c',3), ('a',123), ('b
',6)]
def getMaxDiff(lst):
prev, maxDiff = {}, {}
for t in lst:
if t[0] not in prev.keys():
prev[t[0]] = t[1]
maxDiff[t[0]] = 0
else:
curDiff = t[1] - prev[t[0]]
if curDiff > maxDiff[t[0]]:
maxDiff[t[0]] = curDiff
prev[t[0]] = t[1]
return maxDiff
md = getMaxDiff(lst)
print(lst)
print(md)
输入:
[('a', 2), ('b', 3), (... 阅读全帖 |
|
r**h 发帖数: 1288 | 2 我的想法是,先sort三个数组
然后每个数组从头开始比较三个数的差,然后每次将最小的那个元素的数组指针向后移
动一位。不知道有没有反例啊
run了一个代码看上去是正确的
def getMinDiffTriplet(a, b, c):
minDiff, ma, mb, mc = 9999, -1, -1, -1
pa, pb, pc = 0, 0, 0
while not (pa == len(a)-1 and pb == len(b)-1 and pc == len(c)-1):
na, nb, nc = a[pa], b[pb], c[pc]
curDiff = max(abs(na-nb), abs(na-nc), abs(nb-nc))
if curDiff < minDiff:
minDiff = curDiff
ma, mb, mc = na, nb, nc
if na<=nb and na<=nc:
if p... 阅读全帖 |
|