-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathik5_maximum_sub_array.py
51 lines (41 loc) · 1.13 KB
/
ik5_maximum_sub_array.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
"""
Maximum Sum Subarray
Given a list of integers, find the sum of a non-empty subarray that has the largest sum.
Example One
input: [2, -6, 3, 4, -5]
Output: 7
Example Two
input: [-7, -9, -3, -5]
Output:-3
Notes
A subarray is an array composed of a contiguous block of the original array's elements.
Constraints:
1 <= length of the given list <= 105
-104 <= elements of list <= 104
"""
import unittest
def maximum_subarray(nums: list[int]) -> int:
"""
Time Complexity: O(n)
Space Complexity: O(1)
"""
max_sum = nums[0]
current_sum = 0
for num in nums:
if current_sum < 0:
current_sum = 0
current_sum += num
max_sum = max(max_sum, current_sum)
return max_sum
class TestMaximumSubArray(unittest.TestCase):
"""
Test case for the `maximum subarray` problem.
"""
def test_maximum_subarray(self):
"""
Test case for the `maximum subarray` problem.
"""
self.assertEqual(maximum_subarray([2, -6, 3, 4, -5]), 7)
self.assertEqual(maximum_subarray([-7, -9, -3, -5]), -3)
if __name__ == "__main__":
unittest.main()