-
Notifications
You must be signed in to change notification settings - Fork 1
/
53. Maximum Subarray.py
57 lines (40 loc) · 1.21 KB
/
53. Maximum Subarray.py
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
50
51
52
53
54
55
56
57
from typing import List
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = answer = nums[0]
for curr in nums[1::]:
max_so_far = max(curr + max_so_far, curr)
answer = max(max_so_far, answer)
return answer
""""
Same idea - alot simplier.
No need for so many cases etc...
sample 68 ms submission
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
if not nums:
return None
max_so_far = answer = nums[0]
for i in range(1, len(nums)):
curr = nums[i]
max_so_far = max(curr + max_so_far, curr)
answer = max(max_so_far, answer)
return answer
It turns out it is better to just do inplace replacments, on the array.
It turns out that if you cut on the lines of code in the for loop, this is the thing that does the best changes!
"""
numsnums = [
[-2,1,-3,4,-1,2,1,-5,4],
[1],
[5,4,-1,7,8],
[1,1,1,1],
[-1,-1,-1,-1],
[-1,1,-1,1,-1,1],
[-1],
[-2,-1],
[-1,-2]
]
for nums in numsnums:
res = Solution().maxSubArray(nums)
print(nums)
print(res)