Skip to content

Update problems/53.maximum-sum-subarray-cn.en.md, python code reforma… #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 58 additions & 48 deletions problems/53.maximum-sum-subarray-cn.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,21 @@ _Python3 code_ `(TLE)`

```python
import sys

class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
maxSum = -sys. maxsize
sum = 0
for i in range(n):
sum = 0
for j in range(i, n):
sum += nums[j]
maxSum = max(maxSum, sum)

return maxSum
def maxSubArray(self, nums: list[int]) -> int:
n = len(nums)
maxSum = -sys. maxsize
sum = 0

for i in range(n):
sum = 0

for j in range(i, n):
sum += nums[j]
maxSum = max(maxSum, sum)

return maxSum
```

_Javascript code_ from [**@lucifer**](https://github.com/azl397985856)
Expand Down Expand Up @@ -213,16 +216,16 @@ _Python3 code_

```python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
maxSum = nums[0]
minSum = sum = 0
for i in range(n):
sum += nums[i]
maxSum = max(maxSum, sum - minSum)
minSum = min(minSum, sum)

return maxSum
def maxSubArray(self, nums: list[int]) -> int:
n = len(nums)
maxSum = nums[0]
minSum = sum = 0
for i in range(n):
sum += nums[i]
maxSum = max(maxSum, sum - minSum)
minSum = min(minSum, sum)

return maxSum
```

_Javascript code_ from [**@lucifer**](https://github.com/azl397985856)
Expand Down Expand Up @@ -285,25 +288,31 @@ _Python3 code_
```python
import sys
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
return self. helper(nums, 0, len(nums) - 1)
def helper(self, nums, l, r):
if l > r:
return -sys. maxsize
mid = (l + r) // 2
left = self. helper(nums, l, mid - 1)
right = self. helper(nums, mid + 1, r)
left_suffix_max_sum = right_prefix_max_sum = 0
sum = 0
for i in reversed(range(l, mid)):
sum += nums[i]
left_suffix_max_sum = max(left_suffix_max_sum, sum)
sum = 0
for i in range(mid + 1, r + 1):
sum += nums[i]
right_prefix_max_sum = max(right_prefix_max_sum, sum)
cross_max_sum = left_suffix_max_sum + right_prefix_max_sum + nums[mid]
return max(cross_max_sum, left, right)
def maxSubArray(self, nums: list[int]) -> int:
return self. helper(nums, 0, len(nums) - 1)

def helper(self, nums, l, r):
if l > r:
return -sys. maxsize

mid = (l + r) // 2
left = self.helper(nums, l, mid - 1)
right = self.helper(nums, mid + 1, r)
left_suffix_max_sum = right_prefix_max_sum = 0
sum = 0

for i in reversed(range(l, mid)):
sum += nums[i]
left_suffix_max_sum = max(left_suffix_max_sum, sum)

sum = 0
for i in range(mid + 1, r + 1):
sum += nums[i]
right_prefix_max_sum = max(right_prefix_max_sum, sum)

cross_max_sum = left_suffix_max_sum + right_prefix_max_sum + nums[mid]

return max(cross_max_sum, left, right)
```

_Javascript code_ from [**@lucifer**](https://github.com/azl397985856)
Expand Down Expand Up @@ -359,14 +368,15 @@ _Python3 code_

```python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
max_sum_ending_curr_index = max_sum = nums[0]
for i in range(1, n):
max_sum_ending_curr_index = max(max_sum_ending_curr_index + nums[i], nums[i])
max_sum = max(max_sum_ending_curr_index, max_sum)

return max_sum
def maxSubArray(self, nums: list[int]) -> int:
n = len(nums)
max_sum_ending_curr_index = max_sum = nums[0]

for i in range(1, n):
max_sum_ending_curr_index = max(max_sum_ending_curr_index + nums[i], nums[i])
max_sum = max(max_sum_ending_curr_index, max_sum)

return max_sum
```

_Javascript code_ from [**@lucifer**](https://github.com/azl397985856)
Expand Down