[英]O(N) Time complexity for simple Python function
我剛剛參加了Codility演示測試。 在這里可以看到問題和我的答案 ,但是我也會在這里粘貼我的答案。 我的回復:
def solution(A):
# write your code in Python 2.7
retresult = 1; # the smallest integer we can return, if it is not in the array
A.sort()
for i in A:
if i > 0:
if i==retresult: retresult += 1 # increment the result since the current result exists in the array
elif i>retresult: break # we can go out of the loop since we found a bigger number than our current positive integer result
return retresult
我的問題是時間復雜度,希望您的回答能更好地理解。 問題要求預期的最壞情況下的時間復雜度為O(N) 。
我的函數是否具有O(N)時間復雜度? 我對數組進行排序的事實是否會增加復雜性?
編譯報告(我的回答)
Detected time complexity:
O(N) or O(N * log(N))
那么,我的函數的復雜度是多少? 如果它是O(N * log(N)),那么當問題出現時,我該怎么做才能降低O(N)的復雜度?
非常感謝!
ps我關於時間復雜性的背景閱讀來自這篇很棒的文章 。
編輯
遵循以下答復以及此處針對此問題描述的答案 ,我想在此方面介紹解決方案:
basicSolution的時間復雜度很高,因此對於此Codility測試不是正確的答案:
def basicSolution(A):
# 0(N*log(N) time complexity
retresult = 1; # the smallest integer we can return, if it is not in the array
A.sort()
for i in A:
if i > 0:
if i==retresult: retresult += 1 #increment the result since the current result exists in the array
elif i>retresult: break # we can go out of the loop since we found a bigger number than our current positive integer result
else:
continue; # negative numbers and 0 don't need any work
return retresult
hashSolution是我對上面文章“使用哈希”段落中描述的內容的看法。 由於我是Python的新手,請告訴我您是否對此代碼進行了任何改進(盡管確實適用於我的測試用例),以及它的時間復雜度如何?
def hashSolution(A):
# 0(N) time complexity, I think? but requires 0(N) extra space (requirement states to use 0(N) space
table = {}
for i in A:
if i > 0:
table[i] = True # collision/duplicate will just overwrite
for i in range(1,100000+1): # the problem says that the array has a maximum of 100,000 integers
if not(table.get(i)): return i
return 1 # default
最后,實際的0(N)解決方案(O(n)時間和O(1)額外空間解決方案)讓我難以理解。 我了解到負數/ 0值被推到數組的后面,然后我們得到的數組只是正值。 但是我不理解findMissingPositive函數-有人可以用Python代碼/注釋來描述嗎? 也許有一個例子? 我一直在嘗試用Python來解決它,但無法弄清楚:(
不會,因為您對A
排序。
Python list.sort()
函數使用Timsort (以Tim Peters命名),並且在最壞情況下的時間復雜度為O(NlogN)。
不必對輸入進行排序,而必須對其進行迭代,並通過其他方法確定是否缺少任何整數。 我會使用一組range()
對象:
def solution(A):
expected = set(range(1, len(A) + 1))
for i in A:
expected.discard(i)
if not expected:
# all consecutive digits for len(A) were present, so next is missing
return len(A) + 1
return min(expected)
這是O(N); 我們創建了一個len(A)
(O(N)時間)的集合,然后遍歷A
,從expected
刪除元素(再次為O(N)時間,從集合中刪除元素為O(1)),然后測試expected
為空(O(1)時間),最后得到expected
的最小元素(最多O(N)時間)。
因此,我們在上述函數中最多進行3個O(N)時間步長,使其成為O(N)解決方案。
這也符合存儲要求; 所有使用的都是大小為N的集合。集合的開銷較小,但始終小於N。
您發現的哈希解決方案基於相同的原理,除了它使用字典而不是集合。 請注意,字典值從未實際使用過,它們被設置為True
或不存在。 我將其重寫為:
def hashSolution(A):
seen = {i for i in A if i > 0}
if not seen:
# there were no positive values, so 1 is the first missing.
return 1
for i in range(1, 10**5 + 1):
if i not in seen:
return i
# we can never get here because the inputs are limited to integers up to
# 10k. So either `seen` has a limited number of positive values below
# 10.000 or none at all.
如果A
中沒有正整數,則上述方法避免一直循環到10.000。
mine和他們的區別在於,mine從一組期望的數字開始,而從A
組正值開始,從而反轉存儲和測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.