GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/053_Maximum_Subarray.py at master · crypto318/leetcode

Expand file tree

/

053_Maximum_Subarray.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
48 lines (45 loc) · 1.64 KB
/

053_Maximum_Subarray.py

Copy path

File metadata and controls

48 lines (45 loc) · 1.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution(object):
# def maxSubArray(self, nums):
# return self.maxSubArrayHelper(nums, 0, len(nums) - 1)
#
# def maxSubArrayHelper(self, nums, l, r):
# if l > r:
# return -2147483647
# mid = (l + r) / 2
# leftAns = self.maxSubArrayHelper(nums, l, mid - 1)
# rightAns = self.maxSubArrayHelper(nums, mid + 1, r)
# lMaxSum = res = 0
# for i in range(mid - 1, l -1, -1):
# res += nums[i]
# lMaxSum = max(res, lMaxSum)
# rMaxSum = res = 0
# for i in range(mid + 1, r + 1):
# res += nums[i]
# rMaxSum = max(res, rMaxSum)
# return max(lMaxSum + nums[mid] + rMaxSum, max(leftAns, rightAns))
# def maxSubArray(self, nums):
# """
# :type nums: List[int]
# :rtype: int
# """
# ls = len(nums)
# start = [0] * ls
# all = [0] * ls
# start[-1], all[-1] = nums[-1], nums[-1]
# for i in reversed(range(ls - 1)):
# start[i] = max(nums[i], nums[i] + start[i + 1])
# all[i] = max(start[i], all[i + 1])
# return all[0]
# def maxSubArray(self, nums):
# ls = len(nums)
# start, all = nums[-1], nums[-1]
# for i in reversed(range(ls - 1)):
# start = max(nums[i], nums[i] + start)
# all = max(start, all)
# return all
def maxSubArray(self, nums):
maxEndingHere = maxSofFar = nums[0]
for i in range(1, len(nums)):
maxEndingHere = max(maxEndingHere + nums[i], nums[i])
maxSofFar = max(maxEndingHere, maxSofFar)
return maxSofFar
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.