給定五個分類列表:List1,List2,List3,List4,List5,每個長度爲n。如果有5個(整數)數字(每個列表中有1個),總計爲零返回true。我的目標是確保算法是O(n)。除了我的頭頂,我可以考慮創建一個哈希映射,其中包含5個鏈接列表的總和 或評估5個列表,使得[o(n * n * n * n * n)]。我正在尋找優化或降低複雜性的方法,並且我被卡住了。組合增加爲零
我在Python代碼:
def getIndicesFromFiveArrays(List1,List2,List3,List4,List5,t):
a,b,c,d,e=0,0,0,0,0
while(List1[a]+List2[b]+List3[c]+List4[d]+List5[e]!=t):
b=b+1
if b==len(List2):
return (-1,-1)
if List1[a]+List2[b]+List3[c]+List4[d]+List5[e]<t:
a=a+1
b=b-1
c=c-1
d=d-1
e=e-1
if a==len(List1):
return (-1,-1)
return (a,b,c,d,e)
編輯1:這是不是對了功課,你可以檢查我的其他問題,並驗證自己。謝謝..
它不是作業.. – bouncingHippo 2012-07-19 14:24:55
這些列表是持有浮動還是隻是整數? – NominSim 2012-07-19 14:32:24
這些列表包含整數 – bouncingHippo 2012-07-19 14:33:26