题目描述
blablabla
样例
blablabla
算法1
(DP) $O(n^2)$
blablabla
时间复杂度分析:blablabla
P 代码
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return
dp = [[] for i in range(len(nums))]
MAX = dp[0] = nums[0]
for i in range(1, len(nums)):
dp[i] = max(0, dp[i - 1]) + nums[i]
MAX = max(MAX, dp[i])
return MAX
算法2
(normal) $O(n^2)$
blablabla
时间复杂度分析:blablabla
P代码
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
MAX = - float('inf')
SUM = 0
for i in range(len(nums)):
if SUM < 0: # 说明前面这一段已经可以舍弃
SUM = nums[i]
else:
SUM += nums[i]
MAX = max(MAX, SUM)
return MAX
我冒昧问一下,这个网站的题有源代码吗?我直接把这段复制到Anaconda里出不了结果,不管是C++还是Python大家的答案都会报错在我自己的编译器里。麻烦您解答一下我的疑惑,谢谢啦!