我在跟隨this問題,我覺得這可以在O(NLogN)中解決。以下是我的算法:從三個矩陣算法中找出給定總數「總數」
1. Sort list1 , list2, list3 i.e. O(NLogN)
2. while indexA < size and indexB < size and indexC < size //here size is the array size
int sum = a1[indexA] + a2[indexB] + a3[indexC]
if sum < total then choose the minimum value from a1, a2, a3 and increment that index
if sum > total print total can not be found and return
if sum == total then print the indexes
//this is O(N)
因此,所有總計O(NLogN)。 請告訴我關於上述算法的正確性。
編輯
由於Muckle_ewe解釋說,這種算法中會在一些地方因此在進一步討論的算法中,而請評論的問題是否能爲O解決的是沒有意義的失敗(NLogN)如果是的話,算法,謝謝?
你能提出這個問題添加一些更詳細的嗎?謝謝! – lsk
@ user1930928我有問題的鏈接int。請參閱。 – Trying
「打印總數無法找到並返回」是什麼意思? –