私は3つの配列h1, h2, h3
を持っています。コードの現在のアルゴリズムをO(n + m + k)とすると、どのように最適化できますか?
n1, n2,n3
は、配列の長さを決定します。
すべての配列の最終的な合計が同じになるように、1つ以上の配列から入力を削除する必要があります。
###Inputs
n1,n2, n3 = 5, 3, 4
h1 = 3 2 1 1 1 # 5 elements
h2 = 4 3 2 # 3 elements
h3 = 1 1 4 1 # 4 elements
我々は、上記の配列
sum of h1 = 8
sum of h2 = 9
sum of h3 = 7
配列の先頭からdeletion should happen
に表示された場合。 だからここに私が削除した場合、 3 from h1 (as h1[0])
4 from h2 (as h2[0])
1 and 1 from h3 (as h3[0] and h3[1])
今、すべての(Sum of h1 containts, h2 containts, h3 containts)
合計が5
になります。これは私の最終的な答えです。 以下のコードは、入力リストが小さい場合にうまく機能します。しかし、入力リストがn1 = 100000, n2 = 200000, n3 = 300000
になると。このコードでは、実行に多くの時間がかかります。どうすれば時間を短縮できますか? Pythonのリストの先頭から削除
from collections import *
n1,n2,n3 = input().strip().split(' ')
n1,n2,n3 = [int(n1),int(n2),int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
#print(n1, n2, n3, h1, h2, h3)
h1_tot, h2_tot, h3_tot = sum(h1), sum(h2), sum(h3)
#print(h1_tot, h2_tot, h3_tot)
arr = [h1_tot, h2_tot, h3_tot]
done = False
while len(Counter(arr)) != 1:
if len(Counter(arr)) == 3:
if arr.index(min(Counter(arr))) == 0:
h3.remove(h3[0])
h2.remove(h2[0])
elif arr.index(min(Counter(arr))) == 1:
h3.remove(h3[0])
h1.remove(h1[0])
else:
h1.remove(h1[0])
h2.remove(h2[0])
if len(Counter(arr)) == 2:
index = arr.index(min(arr))
if arr[0] == arr[1] and index == 2:
h1.remove(h1[0])
h2.remove(h2[0])
elif arr[1] == arr[2] and index == 0:
h2.remove(h2[0])
h3.remove(h3[0])
elif arr[0] == arr[2] and index == 1:
h1.remove(h1[0])
h3.remove(h3[0])
if arr[0] == arr[1] and (index == 0 or index == 1):
h3.remove(h3[0])
elif arr[1] == arr[2] and (index == 2 or index == 1):
h1.remove(h1[0])
elif arr[0] == arr[2] and (index == 0 or index == 2):
h2.remove(h2[0])
h1_tot, h2_tot, h3_tot = sum(h1), sum(h2), sum(h3)
arr = [h1_tot, h2_tot, h3_tot]
print(arr[0])