forked from mbobesic/algorithms-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinMaxDivision.py
45 lines (31 loc) · 882 Bytes
/
MinMaxDivision.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
# link: https://codility.com/demo/take-sample-test/min_max_division
# name: Min Max Division
# you can use print for debugging purposes, e.g.
# print "this is a debug message"
def solution(K, M, A):
# write your code in Python 2.7
n = len(A)
M = max(A) # trust no one
end = n*M
beg = 0
result = n*M
while end >= beg:
mid = (beg+end)/2
if check(A, K, M, mid):
end = mid-1
result = mid
else:
beg = mid + 1
return result
def check(A, K, M, possible_sol):
if possible_sol < M:
return False
current_sum = 0
result = 1
for x in A:
if current_sum + x > possible_sol:
result += 1
current_sum = x
continue
current_sum += x
return result <= K