-
Notifications
You must be signed in to change notification settings - Fork 1
/
34.Amazon.py
56 lines (52 loc) · 1.19 KB
/
34.Amazon.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
arr = [34, -50, 42, 14, -5, 86]
def subseq(arr):
i = len(arr)
temp = 0
lst = result = []
while i:
if sum(arr[:i]) > temp:
temp = sum(arr[:i])
lst = arr[:i]
i-=1
i = 0
while i < len(lst):
if sum(lst[i:]) >= temp:
temp = sum(lst[i:])
result = lst[i:]
i+=1
if result == []:
return 0
return result
print(subseq(arr))
def max_sum(array):
temp = 0
maximum = 0
for i in range(len(array)):
for j in range(1, len(array)+1):
if (j > i):
if (sum(array[i:j]) > temp):
temp = sum(array[i:j])
if (temp > maximum):
maximum = temp
return maximum
# Çözüm-1
def f_maxsub(nums):
result = float("-inf")
if len(nums) < 2:
try:
result = max(0,nums[0])
except:
result = 0
else:
return result
for i in range(len(nums)):
for j in range(i+1,len(nums)+1):
result = max(result, sum(nums[i:j]))
return max(0,result)
# Çözüm-2
def f_maxsub2(nums):
result = now = float("-inf")
for i in nums:
now = max(now + i, i)
result = max(result, now)
return max(result,0)