-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.1.MaxDoubleSliceSum.py
57 lines (42 loc) · 1.04 KB
/
9.1.MaxDoubleSliceSum.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
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 26 10:21:04 2017
@author: James
"""
"""
9.1. MaxDoubleSliceSum
Find the maximal sum of any double slice.
https://codility.com/demo/results/trainingUMHPCK-RC4/
"""
import unittest
def solution(A):
N = len(A)
maxToSlice = N * [0]
s = 0
for i in range(1, N - 1):
if i == 1:
s = 0
else:
s = s + A[i - 1]
if s < 0:
s = 0
maxToSlice[i] = s
maxFromSlice = N * [0]
s = 0
for i in range(-2, -N, -1):
if i == -2:
s = 0
else:
s = s + A[i + 1]
if s < 0:
s = 0
maxFromSlice[i] = s
maxSum = -10000
for i in range(1, N -1):
maxSum = max(maxSum, maxFromSlice[i] + maxToSlice[i])
return maxSum
class TestSolution(unittest.TestCase):
def test_solution(self):
self.assertEqual(solution([3, 2, 6, -1, 4, 5, -1, 2]), 17)
if __name__ == '__main__':
unittest.main(exit=False)