-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0053-maximum-subarray.py
More file actions
49 lines (34 loc) · 1.16 KB
/
0053-maximum-subarray.py
File metadata and controls
49 lines (34 loc) · 1.16 KB
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
49
from typing import List
class Solution:
def maxSubArray_tooSlow(self, nums: List[int]) -> int:
max = nums[0]
pointer1 = 0
while pointer1 < len(nums):
pointer2 = pointer1 + 1
while pointer2 <= len(nums):
if sum(nums[pointer1:pointer2]) > max:
max = sum(nums[pointer1:pointer2])
pointer2 += 1
pointer1 += 1
return max
def maxSubArray(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
max = sum(nums[:])
return max
def maxSubArray_example(self, nums: List[int]) -> int:
local_maximum = float('-inf')
global_maximum = float('-inf')
for num in nums:
local_maximum = max(num, num + local_maximum)
global_maximum = max(global_maximum, local_maximum)
print("Kelly is cute")
return global_maximum
def main():
sol = Solution()
input1 = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
input2 = [-5, -3, -1, 1, 3, 5]
input3 = [-1]
print(sol.maxSubArray_example(input1))
if __name__ == "__main__":
main()