Skip to content

Instantly share code, notes, and snippets.

@laonger
Last active December 18, 2015 12:18
Show Gist options
  • Save laonger/5781396 to your computer and use it in GitHub Desktop.
Save laonger/5781396 to your computer and use it in GitHub Desktop.
一个已排好序的数组,求其中两个元素(a和b),使得(a-b)的平方最小(要求复杂度尽可能低)
def a(l):
c, n = l[-1] - l[-2], 0
r = len(l) -2
while n <= len(l) -3:
cc = l[n+1] - l[n]
if cc < c:
r, c = n, cc
n = n +1
return r, r+1
print a([1, 4, 6, 23, 53, 56, 74, 254, 324, 345, 9898])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment