Skip to content

Commit c717f8d

Browse files
taufikalgicclauss
authored andcommittedOct 31, 2019
add max sum contigous subsequence (TheAlgorithms#1537)
* add max sum contigous subsequence * fix typo * Add doctest and type hints * Create autoblack.yml
1 parent 814750e commit c717f8d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
 

‎.github/workflows/autoblack.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# GitHub Action that uses Black to reformat the Python code in an incoming pull request.
2+
# If all Python code in the pull request is complient with Black then this Action does nothing.
3+
# Othewrwise, Black is run and its changes are committed back to the incoming pull request.
4+
# https://github.com/cclauss/autoblack
5+
6+
name: autoblack
7+
on: [pull_request]
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
max-parallel: 1
13+
matrix:
14+
python-version: [3.7]
15+
steps:
16+
- uses: actions/checkout@v1
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v1
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
- name: Install psf/black
22+
run: pip install black
23+
- name: Run black --check .
24+
run: black --check .
25+
- name: If needed, commit black changes to the pull request
26+
if: failure()
27+
run: |
28+
black .
29+
git config --global user.name 'autoblack'
30+
git config --global user.email 'cclauss@users.noreply.github.com'
31+
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
32+
git checkout $GITHUB_HEAD_REF
33+
git commit -am "fixup: Format Python code with psf/black"
34+
git push
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def max_subarray_sum(nums: list) -> int:
2+
"""
3+
>>> max_subarray_sum([6 , 9, -1, 3, -7, -5, 10])
4+
17
5+
"""
6+
if not nums:
7+
return 0
8+
n = len(nums)
9+
s = [0] * n
10+
res, s, s_pre = nums[0], nums[0], nums[0]
11+
for i in range(1, n):
12+
s = max(nums[i], s_pre + nums[i])
13+
s_pre = s
14+
res = max(res, s)
15+
return res
16+
17+
18+
if __name__ == "__main__":
19+
nums = [6, 9, -1, 3, -7, -5, 10]
20+
print(max_subarray_sum(nums))

0 commit comments

Comments
 (0)