728x90
dp 사용
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = [num for num in nums]
for i in range(1, len(nums)):
dp[i] = max(dp[i-1]+nums[i], nums[i])
return max(dp)
dp 미사용
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
# 3/26/2021 microsoft prep
max_sub = 0
global_max = float('-inf')
for n in nums:
max_sub = max(max_sub + n, n)
global_max = max(max_sub, global_max)
return global_max
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Plus One 파이썬 (join, list comprehension) (0) | 2021.03.30 |
---|---|
[LeetCode] Length of Last Word 파이썬 (rstrip, split) (0) | 2021.03.30 |
[LeetCode] Search Insert Position (index) (0) | 2021.03.30 |
[LeetCode] Implement strStr() 파이썬 (exhaustive search, find, index string) (0) | 2021.03.29 |
[LeetCode] Remove Element 파이썬 (list remove) (0) | 2021.03.29 |
댓글