[英]Time complexity for the function maxsubarray
I m having a hard time calculating time complexity for this function since I used sum() for the first time, so sum takes a list sum(list[])
and returns the total sum of that list and has a time complexity of O(n)
.自从我第一次使用 sum() 以来,我很难计算这个函数的时间复杂度,所以 sum 需要一个列表sum(list[])
并返回该列表的总和,时间复杂度为O(n)
。 Also if it's greater than n^2 is there anything I can do to make it n^2 or lower.此外,如果它大于 n^2 有什么我可以做的,使其 n^2 或更低。
def maxsubarray (inputlst):
size = len(inputlst)
minlist = [inputlst[0]]
globallist = [inputlst[0]]
for i in range(1,size):
minlist.insert(i,inputlst[i])
if (sum(minlist) > inputlst[i]):
if sum(minlist) > sum(globallist):
globallist = list(minlist)
if (sum(minlist) < inputlst[i]):
minlist = [inputlst[i]]
if sum(minlist) > sum(globallist):
globallist = list(minlist)
Let's examine your code step by step to find the time complexity.让我们逐步检查您的代码以找出时间复杂度。
class Solution(object):
def maxSubArray(self, inputlst):
size = len(inputlst) # O(1)
minlist = [inputlst[0]] # O(1)
globallist = [inputlst[0]] # O(1)
for i in range(1,size): # O(len(inputlst)) = O(n)
minlist.insert(i,inputlst[i]) # O(n) --> insert time complexity
if (sum(minlist) > inputlst[i]): # O(n) --> sum
if sum(minlist) > sum(globallist): # O(n) --> sum
globallist = list(minlist) # O(n)
if (sum(minlist) < inputlst[i]): # O(n)
minlist = [inputlst[i]]
if sum(minlist) > sum(globallist): # O(n)
globallist = list(minlist) # O(n)
return sum(globallist)
In average, sum()
functions takes O(n)
time complexity since it traverses all the list.平均而言, sum()
函数需要O(n)
时间复杂度,因为它遍历所有列表。 insert(index, element)
also takes O(n)
time complexity, because inserting to a specific index may require remaining elements to be shifted to the right. insert(index, element)
也需要O(n)
时间复杂度,因为插入到特定索引可能需要将剩余元素向右移动。 Creating a list using list()
takes O(n)
time complexity, since again you have to traverse through the list.使用list()
创建列表的时间复杂度为O(n)
,因为您必须再次遍历列表。
In overall, your code performs operations that require O(n)
time complexity within a for loop, that also requires O(n)
time complexity.总的来说,您的代码在 for 循环中执行需要O(n)
时间复杂度的操作,这也需要O(n)
时间复杂度。 Therefore time complexity of your solution will be O(n^2)
.因此,您的解决方案的时间复杂度为O(n^2)
。 It seems to be an inefficient solution for max subarray problem.这似乎是最大子阵列问题的低效解决方案。 Check the more simple, short solution below:检查下面更简单、更短的解决方案:
class Solution:
def maxSubArray(self, nums):
prev = nums[0]
max_ = prev
for i in range(1, len(nums)):
prev = max(prev + nums[i], nums[i])
if(max_ < prev): max_ = prev
return max_
Logic is simple: You should either add the current item to your subarray, or not in order to find the maximum subarray.逻辑很简单:您应该将当前项添加到您的子数组中,或者不添加以找到最大子数组。 It's called Kadane's Algorithm .它被称为Kadane 算法。
Since you just iterate through the list once, time complexity is O(n)
.由于您只是遍历列表一次,因此时间复杂度为O(n)
。 In addition, you don't create unnecessary lists, two variables (one for previous number, other for maximum sum) will be enough.此外,您不要创建不必要的列表,两个变量(一个用于前一个数字,另一个用于最大总和)就足够了。
In addition to the sum, if you want to return the maximum subarray, you can hold two indices corresponding to starting and ending points.除了求和之外,如果要返回最大的子数组,可以持有两个起始点和结束点对应的索引。
class Solution:
def maxSubArray(self, nums):
prev = nums[0]
max_ = prev
start = 0
end = 0
start_max = 0
end_max = 0
for i in range(1, len(nums)):
if(prev + nums[i] >= nums[i]):
prev += nums[i]
end = i
else:
prev = nums[i]
start = i
end = i
if(max_ < prev):
max_ = prev
start_max = start
end_max = end
print(nums[start_max:end_max+1])
return max_
Output for nums = [-2,1,-3,4,-1,2,1,-5,4]
: nums = [-2,1,-3,4,-1,2,1,-5,4]
:
[4,-1,2,1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.