Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def maxSubArray(self, nums: List[int]) -> int:
- n = len(nums)
- dp = [0]*(n+1)
- max_so_far = -math.inf
- for i in range(1, n+1):
- dp[i] = max(dp[i-1] + nums[i-1], nums[i-1])
- max_so_far = max(dp[i], max_so_far)
- return max_so_far
- class Solution:
- def maxSubArray(self, nums: List[int]) -> int:
- rolling_sum = 0
- max_ending_here = -float("inf")
- max_subarray_sum = -float("inf")
- for num in nums:
- # for the current num - do it
- max_ending_here = max(max_ending_here + num, num)
- max_subarray_sum = max(max_subarray_sum, max_ending_here)
- return max_subarray_sum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement