def iscovered(R):#stackglobal K,infq=inf.copy()times=Ktag=Falsewhile times>0:#print(q)if q==[]:breaktarget=q[0]+Rwhile q[0]<=target:#並不是O(n*k)q.pop(0)if q==[]:breaktimes-=1#print(q)if len(q)>=1:return Falseelse:return Truex=list(map(int,input().split()))N=x[0]K=x[1]inf=list(map(int,input().split()))inf.sort()a=[i for i in range(1,(inf[-1]-inf[0]+1)]right=len(a)-1left=0#抓出可以和不可以的交界goodR=inf[-1]-inf[0]#print(a)while right>=left:#print(inf,K,sep=" ")mid=(left+right)//2#print(a[mid])if iscovered(a[mid])==True:goodR=a[mid]right=mid-1else:left=mid+1print(goodR)這行發生memoryerror a=[i for i in range(1,(inf[-1]-inf[0]+1)]
你的a是多餘的,後面a[mid]直接改用mid+1就好了